Home >Web Front-end >JS Tutorial >Handling ajax cross-domain request parsererror error

Handling ajax cross-domain request parsererror error

php中世界最好的语言
php中世界最好的语言Original
2018-04-02 16:30:491909browse

This time I will bring you the handling of ajax cross-domain request parsererror error and the precautions for handling ajax cross-domain request parsererror error. The following is a practical case, let's take a look.

The parsererror error reported by ajax request is a very broad concept. This error is reported in many cases.

In many cases, even if ajax submits and returns normally,

XMLHttpRequest. status=200 (normal response)

XMLHttpRequest.readyState=4 (normal reception)

ajax will also prompt a parseerror error.

appears This error is mostly caused by bad writing habits or improper grammar.

For ajax errors, please use:

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

or:

$("p").ajaxError(function(event,request, settings){
   $(this).append("<li>出错页面:" + settings.url + "</li>");
});

Get error-related information for analysis.

textStatus returns null, "timeout", "error", "notmodified" and "parsererror".

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

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

This standard includes syntax , data format, punctuation, etc.

In ajax cross request, if data is empty, please use;

data:"{}",

When ajax processes the Json format data returned by the server, if the $.parseJSON() method is used,

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

The characters are not Language must be surrounded by double quotes instead of single quotes. Values, Boolean type, null is not used.

This mainly occurs in some old jquery versions with single quotes. An error message appears due to different double quotes.

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

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

Please Note that the semicolon ";" at the end of this function must be added,

Otherwise, if there are multiple ajax requests on the same page , and when the data is not returned and other ajax requests are issued,

a parsererror error message may appear.

This kind of error is very hidden. It is not easy to find during development, but it is easily exposed during concurrent testing.

I believe you have read this article You have mastered the case method. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

How ajax implements the paging query function of bootstrap modal box

Cookie is lost during Ajax cross-domain access How to deal with it

The above is the detailed content of Handling ajax cross-domain request parsererror error. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn