The error code of ajax requests is 0.

Source: Internet
Author: User

The error code of ajax requests is 0.

When ajax is used to request data from the background today, an error occurs, prompting that the status code is 0. The background adopts the spring mvc Architecture.

What does the status code 0 mean? After searching, it means (not initialized) that 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 ("published successfully! "); Window. location. href = 'HTTP: // www.baidu.com '; }}, error: function (XMLHttpRequest, textStatus, errorThrown) {alert (XMLHttpRequest. status); alert (XMLHttpRequest. readyState); alert (textStatus );}});

Check it carefully. It seems that nothing is wrong. Besides, the data can be normally received in the background, indicating that ajax still sends data. This is the parameter information printed in the background.

Another thought turned out to be a problem with the form:

<Form onsubmit = "addBlog ();"> // skipped <button type = "submit"> post a blog </button> </form>

As you can see, I added the type = "submit" attribute to the button tag, but this will generate a new form click submission. Originally, a form will be submitted when the form click button by default, when button type = "submit", a new first submission is generated, resulting in a change in the form event before ajax is executed.

Solution:Change the code above:

<Form onsubmit = "return false"> // <button type = "addBlog ()"> post a blog </button>

The above Article quickly solves the problem that the error code of ajax requests is 0, which is all the content that I shared with you. I hope you can give us a reference and support the help house.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.