0

I'm running asp.net c# and from the server I'm trying to get the status of an application running on the client using port 9192. To go about this I'm calling the below ajax get call. My clients are all vpn connected to the server so I'm using the vpn ip of the client

However I'm getting the below error from the console

XMLHttpRequest cannot load http://clientIP:9192/omxcmd?cmd=position. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://[my server name]:81' is therefore not allowed access.

I have tried a pure javascript call from here HTTP GET request in JavaScript?

with the same error I have also added the below in my web.config file which is supposed to give me the cross domain calls but this hasnt made a difference.

 <httpProtocol>
        <customHeaders>
            <!-- Enable Cross Domain AJAX calls -->
            <remove name="Access-Control-Allow-Origin" />
            <add name="Access-Control-Allow-Origin" value="*" />
        </customHeaders>
    </httpProtocol>

the below is my ajax call from my aspx page

    $.ajax({
    url: "http://VPNIPofCLIENT:9192/omxcmd?cmd=position",
    type: "GET",
    crossDomain: true,
    dataType: "json",
    success: function (response) {
        var resp = JSON.parse(response)
        alert(resp.status);
    },
    error: function (xhr, status) {
        alert("error");
    }
});     

I have also started the chromium browser on the client side with chromium-browser --disable-web-security with no joy

Using the "http://localhost:9192/omxcmd?cmd=position" is returning the correct string back so it is working

AShah
  • 846
  • 2
  • 17
  • 33
  • 2
    Possible duplicate of [How to implement "Access-Control-Allow-Origin" header in asp.net](https://stackoverflow.com/questions/6516591/how-to-implement-access-control-allow-origin-header-in-asp-net) – Heretic Monkey Jul 08 '18 at 20:44
  • It seems a bit odd to have the server calling a client using HTTP. Typically it's the other way around, and it makes it harder to talk about client/server relationships. It's not clear from where your ajax call is happening. If it's from the application running on the client, you wouldn't be getting that error, since the app would be calling itself, but if it's from an application running on the server, you wouldn't be using `localhost`, as that would loopback to the server, not the client. – Heretic Monkey Jul 08 '18 at 20:52
  • you correct localhost is wrong i'm (question edited) I'm using the ip address of the client – AShah Jul 08 '18 at 20:58
  • I just need to get a check if a player is running on the client (its a kiosk type setup) and the only way of doing it is using as http request to the client on port 9192 – AShah Jul 08 '18 at 21:24

1 Answers1

0

The same problem was occurs me once and i solved in one way i explain..

First you need to enable cors in your web api server side..

First you need to install

Install-Package Microsoft.AspNet.WebApi.Cors

In Nuget Package manager console in your webapi project..

and then add two more lines in your web api project WebConfig.cs file

public static void register(HttpConfiguration config){
     EnableCorsAttribute cors=new EnableCorsAttribute("your-client-localhost-url-with-port","your-webapi-localhost-port","any-methods");
     config.EnableCors(cors); 
}


for example :
 EnableCorsAttribute cors = new
 EnableCorsAttribute("http://localhost:8080","http://localhost:1995");
//only allowed given local host

 EnableCorsAttribute cors = new 
 EnableCorsAttribute("*","*","*"); //allow all applications to access web api

I hope this was helpful to you...

Mohideen Asraf
  • 32
  • 1
  • 1
  • 8