Ajax request status Code of 0 workaround

Source: Internet
Author: User

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

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.