Home  >  Article  >  Web Front-end  >  How to deal with the ajax request error status code is 0

How to deal with the ajax request error status code is 0

php中世界最好的语言
php中世界最好的语言Original
2018-03-30 15:44:372990browse

This time I will bring you an ajax request errorHow to handle when the status code is 0, what are the precautions when handling the ajax request error status code is 0, the following are Let’s take a look at practical cases.

Today, an error occurred when using ajax to request data from the background. The prompt status code was 0. The background uses spring mvc Architecture.

What does the status code 0 mean? After searching, it turns out that it means (not initialized), that is, the send() method is not called. My original code is as follows:

$.ajax({
url:"test",
type:"post",
data:{
  blogTitle : $("#form1 input").val(),
  blogType : $("#form1 option:selected").val(),
  article : htmlcontent
},
dataType: "json",
success: function(data,textStatus){
  if(data.flag == "success"){
    alert("发表成功!");
    window.location.href = 'http://www.baidu.com';
  }    
},
error: function(XMLHttpRequest, textStatus, errorThrown){
  alert(XMLHttpRequest.status);
  alert(XMLHttpRequest.readyState);
  alert(textStatus);
}
 });

After careful inspection, it seems that there is nothing wrong, and it can receive and send normally in the background. data, indicating that ajax still sent the data. This is the relevant parameter information printed in the background.

After some thought, it turned out that the form appeared. Problem:

<form onsubmit="addBlog();">
//中间省略
<button type="submit">发表博客</button> 
</form>

As you can see, I added the type="submit" attribute to the button tag, but doing so will generate a new form click to submit. Originally, the form clicked the button by default. A submission will be generated when button type="submit" is used, and a new first submission will be generated when button type="submit" is used, causing the ajax unexecuted form event to change.

Solution: Change the above code to:

<form onsubmit="return false">
//中间省略
<button type="addBlog()">发表博客</button>

I believe you have mastered the method after reading the case in this article. Please pay attention for more exciting things. Other related articles on php Chinese website!

Recommended reading:

Ajax+PHP code to change status and delete without refreshing

How to implement Ajax client Asynchronously calling the server

The above is the detailed content of How to deal with the ajax request error status code is 0. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn