Perfect solution for parsererror errors in ajax cross-origin requests, ajaxparsererror

Source: Internet
Author: User

Perfect solution for parsererror errors in ajax cross-origin requests, ajaxparsererror

The parsererror message for ajax requests is a broad concept. In many cases, this error is reported,

In many cases, even if ajax is submitted and returned

XMLHttpRequest. status = 200 (normal response)

XMLHttpRequest. readyState = 4 (normally received)

Ajax will also prompt a parseerror error.

This error is mostly caused by poor writing habits or improper syntax.

For ajax errors, use:

error:function(XMLHttpRequest, textStatus, errorThrown){   $("div").html(textStatus);   $("div").append("<br/>"XMLHttpRequest.status);   $("div").append("<br/>"XMLHttpRequest.readyState);   $("div").append("<br/>"XMLHttpRequest.responseText);}

Or:

$ ("Div "). ajaxError (function (event, request, settings) {$ (this ). append ("<li> error page:" + settings. url + "</li> ");});

Obtain error information for analysis.

In addition to null, "timeout", "error", "notmodified", and "parsererror", textStatus is returned ".

The XMLHttpRequest object can use attributes such as status, readyState, and responseText to obtain the HTTP code, process the status, and the text returned by the server.

When using ajax and related application development, you must write programs according to the standard to reduce the chance of errors.

This standard includes syntax, data format, punctuation, and so on.

Cross-request ajax. If data is empty, use it;

Data :"{}",

When the ajax processing server returns Json data, if the $. parseJSON () method is used,

The Json data format returned by the server must be written in the standard Json format,

For characters, double quotation marks must be used instead of single quotation marks. Values, Boolean types, and null values are not required,

In some old jquery versions, single quotation marks and double quotation marks are used to indicate errors.

In ajax cross-origin request (jsonp), the format of data returned by the server must be:

MyCallbackFunction ({"id": 1, "name": "C #", "is_familiar": true });

Note that the last Semicolon ";" of this function must be added,

Otherwise, if the same page has multiple ajax requests and other ajax requests are sent when no data is returned,

The error message "parsererror" may occur.

This kind of error is very hidden. It is not easy to discover during development, and it is easy to expose During Concurrent testing.

The preceding article perfectly solves the parsererror error in cross-origin ajax requests. It is all the content shared by the editor. I hope you can give us a reference, and I hope you can provide more support to the customer center.

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.