JQuery-Ajax Headers Problem

I am having a problem with headers when I try to execute a POST request using JSON
This is the code:

$.ajax({ type: "POST", url: url, data: jsonData, dataType: 'json', beforeSend: function(xhrObj){ xhrObj.setRequestHeader("Content-Type","application/json"); xhrObj.setRequestHeader("Accept","application/json"); }, error: function(){ alert("Fail"); }, success: function(){ alert("Success"); } }); 

And this is the request header displayed by Firebug.

 OPTIONS /path HTTP/1.1 Host: 192.168.15.109:8080 User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0 FirePHP/0.7.1 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip, deflate DNT: 1 Connection: keep-alive Origin: http://localhost Access-Control-Request-Method: POST Access-Control-Request-Headers: content-type x-insight: activate Pragma: no-cache Cache-Control: no-cache 

And the response headers:

 HTTP/1.1 204 No Content Date: Thu, 24 May 2012 19:17:01 GMT Allow: OPTIONS,POST 

As you can see, the headers do not match the names given to them, but when I use CURL, the headers instead:

 POST /path HTTP/1.1 User-Agent: curl/7.25.0 (i386-pc-win32) libcurl/7.25.0 OpenSSL/0.9.8u zlib/1.2 Host: localhost:8080 Accept: */* Content-Type: application/json Content-Length: 5 

Any idea or solution for this?

I also modified JQuery Source to set the default values ​​of the headers sent by Ajax to JSON, but it did not work.

+1
source share
2 answers

Actually, it was a cross domain problem, I defined my URL as IP, so the browser interpreted it as a cross domain request.

Thanks for all!

+1
source

Sounds like a problem with the same source. Using dataType='jsonp' should work, but this may require other changes.

See https://developer.mozilla.org/en/http_access_control for a detailed explanation.

+1
source

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


All Articles