bentwonk
bentwonk

Reputation: 193

jquery $.ajax get will fetch a file via http but fails when talking to server direct, X-Requested-With not being set correctly?

I am having trouble getting jquery (1.6.1) to talk to a local server (on same machine) even though as far as I can tell what the server is producing looks correct.

as a test I put the server output in a file, and access it, it works:

var url = "http://localhost/web-from-hols/viz/library/test4.json"

$.ajax({
        url: url,
        dataType: "json",
        cache: false,
        type: 'GET',
        error: that.searchError,
        success: that.searchSuccess,
        complete: function(){dom.find('#loadingImg').hide();dom.find('.button').show();searching=false;}
            })

but... point the url directly at the local server, and it fails, with status 0

var url = "http://127.0.0.1:8080/"+key

sniffing the http headers I see the following call and response,for the working...

http://localhost/web-from-hols/viz/library/test4.json?_=1333363765977

GET /web-from-hols/viz/library/test4.json?_=1333363765977 HTTP/1.1 Host: localhost User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:11.0) Gecko/20100101 Firefox/11.0 Accept: application/json, text/javascript, */*; q=0.01 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip, deflate DNT: 1 Connection: keep-alive X-Requested-With: XMLHttpRequest Referer: http://localhost/web-from-hols/viz/viz.html


HTTP/1.1 200 OK Date: Mon, 02 Apr 2012 10:49:25 GMT Server: Apache/2.2.20 (Ubuntu) Last-Modified: Mon, 02 Apr 2012 02:28:15 GMT Etag: "42e020c-137f-4bca8f1467744" Accept-Ranges: bytes Content-Length: 4991 Keep-Alive: timeout=5, max=100 Connection: Keep-Alive Content-Type: application/json

and for the non working I see:

http://127.0.0.1:8080/EP1186609?_=1333370799152

GET /EP1186609?_=1333370799152 HTTP/1.1
Host: 127.0.0.1:8080
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:11.0) Gecko/20100101 Firefox/11.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
Referer: http://localhost/web-from-hols/viz/viz.html
Origin: http://localhost


HTTP/1.1 200 OK
Date: Mon, 02 Apr 2012 02:33:43 GMT
Server: Apache/2.2.20 (Ubuntu)
Last-Modified: Mon, 02 Apr 2012 02:28:15 GMT
Etag: "42e020c-137f-4bca8f1467744"
Accept-Ranges: bytes
Content-Length: 4991
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/json

the only real difference I can see is the working request includes

X-Requested-With: XMLHttpRequest

which is missing in the non working, although the non working has added:

Origin: http://localhost

I have tried to add this wit the code

 $.ajaxSetup({ 
      headers: {"X-Requested-With":"XMLHttpRequest"} 
    }); 

but this does not seem to help, rather than add the line

X-Requested-With: XMLHttpRequest

I instead see the following added

Access-Control-Request-Method: GET
Access-Control-Request-Headers: x-requested-with

Frankly I do not understand what's going on, I have read that jquery sometimes omits the 'X-Requested-With: XMLHttpRequest" value when making request, or it might be a cross site issue, might.. :-(

This problem has been frustrating two of us for a day, any help greatly appreciated.

Upvotes: 1

Views: 636

Answers (1)

Tgr
Tgr

Reputation: 28200

You cannot send normal AJAX requests to another domain (even when that domain resolves to the same machine) or a different port. Firefox is trying to send a CORS request, but then stops it after your server does not return the headers which signal that you allow CORS requests (such as Access-Control-Allow-Origin: *).

Upvotes: 2

Related Questions