JQuery answer is empty in browser though curl works

I am using jQuery .ajax() to call the server (actually the local Django server) and get a response.

On the server console, I see that the JSON request is coming in, it is responsible for the JSON, and everything looks fine.

But in my browser (tested on Firefox 3.6 and Safari 4.0.4, and I'm using jQuery 1.4.2) it seems that the response body is empty (the response code is 200, and the headers otherwise look OK).

Testing the response from the command line, I get the answer that I expect.

$ curl http://127.0.0.1:8000/api/answers/answer/1 --data "text = test & user_name = testy & user_location = testyville & site = http% 3A% 2F% 2Flocalhost% 3A8888% 2Fcs% 2Fjavascript_answer_form .html & email_address = "
{"answer_permalink": " http://127.0.0.1:8000/questions/1 ", "answer_id": 16, "question_text": "What was the sky like when you were young?", "answer_text": "test" , "question_id": "1"}

I am making a request from an HTML file on my local computer that is not served by a web browser. It simply accessed using file:// . The django server is also local, by default 127.0.0.1:8000.

Thanks for any suggestions!

-Jeet

+4
source share
3 answers

Unless you specifically allow your alternate browser settings for local files, everything remains bound by the cross-domain security policy. Files that do not belong to a domain (for example, localhost ) cannot request files from this domain.

I'm not sure how cross-domain policy works with ports; you can place this file in the localhost folder available on port-80 (if any) and do the job. Otherwise, you are stuck if you cannot change your browser settings to make exceptions (and even then I'm not sure that this can be done in any standard browsers).

+2
source

Add the function "error: function (data) {alert (data);}" to check if your $ .ajax has worked.

+1
source

Change 'complete' to 'success' in your .ajax () call. "complete" is used to signal when an ajax operation is in progress, but does not provide response data. Success is called with a successful request and receives a response. "error" is a success symbol used to handle errors.

I think that browsers (at least some, for example Safari, for me) consider files submitted from the file system to be reliable sources in terms of policies of the same origin. Thus, there was a red herring.

+1
source

Source: https://habr.com/ru/post/1303162/


All Articles