0

I need to send a GET request to a file on the local network. But I always get a No Acces-Control-Allow-Origin header. I am doing this from my pc already in the local network. But it is the same when I do this over my domainname. I am doing this in javascript with an Ajax-call to a php script.

this is my ajax-call:

$.ajax({
    data: {},
    url: 'http://192.168.2.100/php/checkForHomeNetwork.php',
    method: 'GET',
    datatype: 'json',
    async: false,
    cache: false,
    timeout: 30000,
    success: function(msg) {
        alert(msg);
    }
});
vanlooverenkoen
  • 2,121
  • 26
  • 50

1 Answers1

2

The same-origin policy doesn't care about what network you're on. If the domain portion of the URL (including "http"/"https", the domain name string, and port number) are not exactly the same, then it's considered to be a cross-domain request.

The PHP script can of course be made to include the appropriate headers when it sees a request from that trusted domain.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Yes i have read that somewhere but can't how I need to add the other domains and portnumers – vanlooverenkoen Jan 18 '16 at 21:59
  • @KoenVanLooveren it's something you have to do from the PHP service you're invoking. [Here is some information.](https://remysharp.com/2011/04/21/getting-cors-working) You can probably find many other examples out there. – Pointy Jan 18 '16 at 22:17
  • But then it is serverside? I need to check if the client can reach that php file. Not the server – vanlooverenkoen Jan 18 '16 at 22:19
  • But that is serverside? Isn't it? – vanlooverenkoen Jan 18 '16 at 22:20
  • You can't do it from the client. The browser will simply disallow access to different domains *unless* the target URL responds with correct headers. It's a security measure and the browser deliberately stops you from solving the problem in the client (JavaScript) code. – Pointy Jan 18 '16 at 22:27