JQuery, XmlHttpRequest and status code 0

I recently did some cross-domain javascript using JSONP and ASP.NET MVC.

A specific controller action will only respond to a POST request, this is by design.

In IE8, I see (via Fiddler2) that the answer is correct, and it returns an HTTP 200 response along with JSONP javascript.

In Firefox, Safari, and Chrome, the response is still returned with the corresponding HTTP 200 code and JSONP content, the only difference is that the XmlHttpRequest object used by JQuery sets the status code to 0, and the responseText to delete.

Initially, I thought that this was due to preliminary coverage of COR HTTP (Http Access Control), as a result of which a custom header or a content type other than text / plain will send an additional HTTP request (using OPTIONS) to the server. I can see in Fiddler2 that HTTP 404 responds to an OPTIONS request.

The web server is IIS7 (but the production web server will be the IIS6 box). In IIS7, I see the standard OPTIONSVerbHandler specified in the handlers, but I'm not sure if this is actually doing anything (in fact, I can't even find the documentation about OPTIONSVerbHandler anywhere).

To get around this, I modified the JQuery library so as not to set a custom header, and also change the content type in text / plain instead of application / json, and Firefox finally starts to bypass OPTIONS request and just POST.

- ( XmlHttpRequest), Fiddler2 , HTTP 200 .

?

+3
4

, JQuery, POST ( , script ). GET , .

JQuery, , .

Matt

+4

firebug Firefox, . net, HTTP- . , - ? jsonview firefox JSON, appcaiton/json mimietype. , JSONP, .

+2

. Firefox OPTION :

Firefox:

OPTIONS /MvcApplication/Json/Test1 HTTP/1.1
Host: acoheni580
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8
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
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Origin: http://localhost
Access-Control-Request-Method: POST

Mvc , , POST [HttpPost]

:

//[HttpPost]
[AcceptVerbs(new string[] {"POST","OPTIONS"})]
+1
source

Besides all the obvious client-side errors, the main reason for this is that the gecko engine is looking Access-Control-Allow-Originin the header from servlet. If he does not find him, he will disconnect, and you will receive status=0and statusText=null. Also, an error moz-nullprincipalin the XML parsing. All of these things are very misleading. All you need to solve this problem:

response.setHeader("Access-Control-Allow-Origin","*");

In code servlet, life will be good :-)

+1
source

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


All Articles