Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Ajax request from local filesystem (Windows file:///)

I'm trying to do an ajax request to get the contents of "http://localhost/" running on Windows Wamp Server.

The script is running from something like this:

file:///C:/my/path/index.html

I'm just using a standard $.ajax request to try and get the contents of localhost:

$.ajax({
          type: 'GET', 
          url: 'http://localhost/',
          success: function(data) {
            alert('success');
          }, error: function (data) {
            alert('failed');
          }
    });

I can't get it to be successful though... Seems to be some problem with the local filesystem or something. I'm not too sure.

like image 253
Emmanuel Avatar asked Mar 29 '11 07:03

Emmanuel


2 Answers

Problem Solved!

I just had to add this header to my index.php file for http://localhost/

header('Access-Control-Allow-Origin: *');

Thanks for your help anyhow guys!

like image 161
Emmanuel Avatar answered Sep 19 '22 18:09

Emmanuel


You say that the script is running from a file:/// URL. It's best not to do AJAX requests from file URLs, because they are treated inconsistently. Chrome, for example, entirely disallows them.

However, your bigger problem here is the same-origin policy: you can only make AJAX requests to the same host as the web page itself. file:/// and http://localhost are not the same host (even if they are the same machine).

It's best to run everything off http://localhost.

like image 32
lonesomeday Avatar answered Sep 21 '22 18:09

lonesomeday