Ajax request status code is 0 solution

<h2>Foreword</h2>

<p>I encountered a strange problem today. Using JQuery's ajax request, the controller layer in the background also received the request. Everything was processed normally, but it was very strange. The browser Debug found that the responseText was "error" and the status code was 0 instead of 200.</p>

<p><strong>The request source code is as follows:</strong></p>

<pre>$.ajax({ type: "POST", url: targetUrl, cache: false, data:post_data, dataType:"json", async: true, // 默认为异步请求 error: function(error) { console.log(error); }, success: function(data) { //... } });</pre>

<h2>Status meaning</h2>

  • 0 - (uninitialized) the send() method has not been called
  • 1 - (Loading) send() method has been called, request is being sent
  • 2 - (loading completed) send() method execution completed
  • 3 - (Interactive) Parsing response content
  • 4 - (Complete) The response content is parsed and can be called on the client side

<h2>Problem Analysis</h2>

<p>jQuery ajax request error returns status 0 and error error, analyze the possible reasons:</p>

  • url does not exist (excluded) - Controller layer defined correctly
  • url unreachable (excluded) - the backend did receive the request and handled it correctly
  • A cross-origin request was sent (excluded) - with CORS Filter, the test found neither
  • Malformed data (excluded) - Browser and background debugging found that the data is in the correct format
  • The ajax request was canceled before it was completed (the ajax request was not made) - indeed canceled, but the request was indeed made
  • Request timed out - acknowledging the problem

<h2>Solution</h2>

  • Use asynchronous requests
  • Set the timeout duration a little longer

<pre>$.ajax({ type: "POST", url: targetUrl, cache: false, data : post_data, // pass parameter dataType : "json", async: false, // use synchronous operation timeout : 50000, / /timeout: 50 seconds error: function (error) { console.log(error); }, success: function (data) { //... } }); </pre>

<p style="font-size:12px;color:#999;">Copyright Statement: The author writes a blog for the purpose of summarizing experience and exchanging learning. If you need to reprint, please give the original link in a prominent position on the article page. Thanks! If you have any questions, please leave a message!

<br>https://blog.csdn.net/changqing5818/article/details/53932463<p>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325323913&siteId=291194637