jeudi 31 mars 2016

Setting up 'Access-Control-Allow-Origin' for HTTP Listener

I wanted a solution to send data from a C# application to a Web Page once a request was made to the C# application (via a AJAX post from the web page), then using the project implemented here, I wrote a simple console application to listen for ajax posts. It works great - I click on a button on my page that fires a AJAX post to http://localhost:8081/, which is the port the HTTP Listener is listening on, then the Listener Callback responds with some data back to the web page and I can use the data in the success function of the ajax post.

My problem - I need to open my webpage by using the URL http://localhost:8081/index.html which request the page from my IIS server, then only can I start my Listener and press the button to fire the AJAX because if I just keep my service running and navigate to the url my page doesnt show up but instead the data my Listener returns and not page IIS should serve. This happens becuase the port my Listener is listening on is the same one IIS is binding to when serving my page. So I decided to keep my IIS binding on port 8081 and just change my Listener to Listen on 8083 instead and let my ajax post to 8083 as well, that way I can keep my IIS service separate from the posts I'm doing to get data.

Here is my listener config:

listener = new HttpListener();
listener.Prefixes.Add("http://localhost:8083/");
listener.Prefixes.Add("http://127.0.0.1:8083/");
listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;

And here is my AJAX Post

.ajax({
      type: "POST",
      url: 'http://localhost:8083/',
      data: {},
      success: function(data) {

        //Use data

      }
      });

This setup should work, only problem is when I now click the button I get the error XMLHttpRequest cannot load http://localhost:8083/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8081' is therefore not allowed access. The response had HTTP status code 503., which is to be expected, but I do not how to fix it.

I read here that:

For each resource/page that Site B wants to make accessible to Site A, Site B >should serve its pages with the response header:

'Access-Control-Allow-Origin: http://siteA.com'

Which is all good but I have no idea where to configure this, and where in my process cycle this specification should happen. Where do i work with the headers?




Aucun commentaire:

Enregistrer un commentaire