3

I've following function:

$.ajax({url:"http://127.0.0.1:8080", data: "123",
    success:
    function(response, textStatus, jqXHR) 
    {
        alert(response);
    },
    error:
    function(jqXHR, textStatus, errorThrown)
    {
        alert("The following error occured: " + textStatus + " " + errorThrown);
    }
});

It sends request and i intercept one with "nc -l 8080". It looks like

GET /?123 HTTP/1.1
Host: 127.0.0.1:8080
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:15.0) Gecko/20100101         Firefox/15.0.1
Accept: */*
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Origin: null

Then i answer with

HTTP/1.1 200 OK
Content-Length: 3

abc

The problem is i always get error ("The following error occured: error") What do you think could be wrong with it?

user1261347
  • 315
  • 2
  • 15

2 Answers2

0

As Tomasz pointed out its because of same origin policy.

To prove/test this:

Assuming your html page is "index.html" do the following.

  • Run command "python -m SimpleHTTPServer 8080" from the same directory as index.html
  • Visit browser and type "http://127.0.0.1:8080/index.html". Python should servet the html and browser should render it fine.
  • Kill the python command now and instead run "nc -l 8080". NC is ready to handle the ajax request, on the same domain/port as the original webpage.
  • Now just invoke the ajax call from webpage, and reply with nc.

The ajax should work now.

therealsachin
  • 1,684
  • 1
  • 14
  • 9
0

readyState=0 means the request was not even sent yet, the name of the constant "UNSENT": http://www.w3.org/TR/XMLHttpRequest/

As to the actual cause of the error before sending out the request, yes, same-origin-policy is the most likely culprit.

You had to use a relative link, url:"/" I guess, to capture a request.

Szocske
  • 7,466
  • 2
  • 20
  • 24