0

I'm trying to exchange data between a web page and a c# socket. The c# socket is running on the localhost. The webpage runs on a server and points to the localhost.

When the webpage sends a Get request to the c# socket an cross-domain error is shown.

XMLHttpRequest cannot load http://localhost:12345/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.1.3:9000' is therefore not allowed access.

This is the JS running on the web page (Angular).

var serviceUrl = 'http://localhost:12345';
var service = $resource(
   serviceUrl,{}, {
       getCard: {
           method: "GET"
       }
   }
);

service.getCard();

This is a part of the code from the c# console application.

private static void Send(Socket handler, String data)
{
    // Convert the string data to byte data using ASCII encoding.
    byte[] byteData = Encoding.ASCII.GetBytes(data);

    // Begin sending the data to the remote device.
    handler.BeginSend( byteData
                     , 0
                     , byteData.Length
                     , 0
                     , new AsyncCallback(SendCallback)
                     , handler                      
                     );

How can i add header information to the response. The headers must be: Access-Control-Allow-Origin: * It doesnt work when i add it in front of the string.

1 Answers1

0

The header has to be added when you serve the page, not from the socket response. It's just a plain old HTTP header.

If you use IIS to serve your web page, do something like this before sending the page:

HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158