Original link: 53932463 preface
Today encountered a strange problem, using jquery Ajax request, the controller layer in the background received the request, all the processing is normal, but it is strange, browser debug found that ResponseText is "error", the status code is 0 instead of 200.
Request source code as follows
$.ajax ({ type: "POST", url:targeturl, cache:false, data:post_data, dataType: "JSON" , async:true,//default is asynchronous request error:function(Error) { console.log (error); }, success : function
State meaning
- 0-(uninitialized) has not yet called the Send () method
- 1-(load) called the Send () method, sending the request
- 2-(loading complete) the Send () method performs the completion
- 3-(interactive) parsing response content
- 4-(complete) The response content resolution is complete and can be invoked on the client
Problem analysis
Query Ajax request Error returns status 0 and error errors to analyze possible causes:
- URL does not exist (excluded) –controller layer is defined correctly
- URL unreachable (excluded) – the backend did receive the request and handled the correct
- Cross-domain request sent (excluded) – using Cors Filter, test discovery is not
- Data format error (exclude) – Browser and background debugging found, data format is correct
- Ajax request has been canceled before completion (AJAX request not issued) – indeed canceled, but the request did
- Request timed out – confirmation issue
Solutions
- Using asynchronous requests
- Set timeout length a little longer
$.ajax ({ type: "POST", url:targeturl, cache:false, data:post_data,//Parameter dataType: " JSON ", async:false,//Use synchronous operation timeout:50000,//timeout time: 50 seconds error:function(error) { Console.log (Error); }, success:function
Copyright notice: The author writes Bo is to sum up experience, and exchange learning. If you want to reprint, please give the original link in the article page obvious location. Thank you! If you have any questions, please leave a message!
Ajax request status Code of 0 workaround