Reason for Ajax execution failure

Source: Internet
Author: User

Some reasons for Ajax jumping into error

Let's start with a standard jquery ajax code:

$.ajax ({        type: ' POST ',        URL: ' getsecondclassification ',        data: {"Sort2": Sort2, "Sort3": Sort3)},        DataType: ' json ',        success:function (sort) {            $ ("#noneSelect"). Remove ();            var optionstring = "";            for (var i in sort) {                optionstring + = "<option value=\" "+ Sort[i].classid +" \ ">" + sort[i].classname + "</ Option> ";            }            $ ("#secondtype"). HTML (optionstring);            $ ("#build"). Removeattr ("Disabled");        },
Error:function (XMLHttpRequest, Textstatus, Errorthrown) {// $ ("#p_test"). InnerHTML = "There is something wrong!"; / alert (xmlhttprequest.status);// alert (xmlhttprequest.readystate);// alert (textstatus); } });

Any errors will jump to the error function.

Illustrate some of the causes of errors:

1. DataType error

Type error: The DataType type returned in the background and the inconsistency of the foreground write will jump into the error such as returning the string DataType: ' JSON ', on the server, sometimes there is a problem, go to error

Format error: The JSON format requirements are very strict after jquery1.4, and JSON format errors also jump into error. {"Test": 1} Note format

Sometimes, when the return value is not required, throw in the format of the template, set the datatype: "JSON", parameters; At this time, when the value of the Ajax is correct, 200 returned to the success of the error status of the special case.

2. Async Request Synchronous Async problem

Async defaults to True (asynchronous request), and if you want to execute another AJAX after an Ajax execution, you need to put the Async=false

For example, you use a POST request to pass a value to another page background, but the page once loaded your Ajax has been executed, the value receive is completed in the background, the request is not the data, so you can consider the AJAX request to synchronize to try.

3. Data cannot be written

The data is empty and must be passed "{}", otherwise the return is in XML format. and prompt ParserError. Data: "{}"

The exception of ParserError is also related to the header type. and coded header (' content-type:text/html; Charset=utf8 ');

4. Parameters passed

Must be an AJAX-supported encoding format

5. URL path Issues

Path cannot have Chinese

To debug the error message using error:

You can alert them and see what's wrong.

Specific parameters:

XMLHttpRequest:XMLHttpRequest.readyState: The meaning of the status code 0-(uninitialized) has not yet called the Send () Method 1-(loaded) called the Send () method, is sending the request 2- (load complete) the Send () method executes, has received full response content 3-(interaction) is parsing the response content 4-(completion) Response content resolution is completed, you can call the
XMLHttpRequest: Xmlhttprequest.status:


textStatus: Cause of error

nulltimeout error notmodified parsererror

Errorthrown:(可选)捕获的错误对象

Reason for Ajax execution failure

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.