简体   繁体   中英

HTTP GET Response parse error (empty String?) - but body is not empty

I am working on a Javascript snippet that communicates with a local Java Server with the help of HTTP Requests.

My requests looks like this:

var req = $.ajax({
    type : "GET",
    dataType : "json",
    url : "http://127.0.0.1:8081", 
    data : data_dict
});

The response header, that I send from my Java Server, seems to be okay (at least the Firefox Web Developer tells me so). But there is an error on body parsing:

JSON.parse: unexpected end of data at line 1 column 1 of the json data

Seems like my response is empty... but when I open http://127.0.0.1:8081/?test_request_key=test_request_value it shows the expected body in my browser:

{"test_respond_key":"test_respond_val"}

Do you have a hint for me? I have no idea where to search for the problem...

EDIT: The complete Response is

HTTP/1.1 200 OK
Server: Java HTTPStudyServer
Content-Type: application/json
Content-Length: 39
Connection: close

{"test_respond_key":"test_respond_val"}

Thanks in advance! (and sorry if this is a noob problem with a simple answer, but some hours of googling didn't help me out)

Okay, I found the solution (with your help!)

The problem was indeed CORS! If you look at the examples at http://de.wikipedia.org/wiki/Same-Origin-Policy you see that for another port it does not count as same origin !

This post gave me proof: Why is same origin policy kicking in when making request from localhost to localhost?

So I simply added this line to the Response Header :

Access-Control-Allow-Origin: *

Now it works like a charm! :)

Thank you for your help guys!

Greetings

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM