English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Quickly solve the problem of AJAX request error status code 0

An error occurred while using ajax to request data from the background, prompting the status code is 0, and the background uses the spring mvc architecture.

What does the status code 0 mean? I searched and found out that it means (not initialized) that the send() method has not been called, and my original code was 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);
}
 });

Upon careful inspection, it seems there is nothing wrong, and it can also be received and sent normally in the background, indicating that ajax has sent the data. This is the relevant parameter information printed by the background

After some thought, it turned out that the form was the problem:

<form onsubmit="addBlog();">
//The middle is omitted
<button type="submit">Post Blog</button> 
</form>

As can be seen, I added the type=”submit” attribute to the button tag, but doing so will cause a new form submission click event, as the form will generate a submission by default when clicking the button, and the button type=”submit” will also generate a new first submission, causing the form event to change before the ajax is executed.

Solution: Change the above code to:

<form onsubmit="return false">
//The middle is omitted
<button type="addBlog()">Post Blog</button> 

This article about quickly solving the problem of the ajax request error status code 0 is all the content shared by the editor. I hope it can be a reference for everyone and I hope everyone will support the Shouting Tutorial.

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been edited by humans, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please feel free to send an email to: notice#w3If you find any copyright infringement, please send an email to: notice#w with the relevant evidence. Once verified, this site will immediately delete the infringing content.

You May Also Like