Home > Web Front-end > JS Tutorial > Perfect solution to parsererror error under ajax cross-domain request

Perfect solution to parsererror error under ajax cross-domain request

PHPz
Release: 2020-07-10 17:29:15
Original
11268 people have browsed it

Now I will bring you an article that perfectly solves the parsererror error under ajax cross-domain request. Let me share it with you now and give it as a reference for everyone.

What does parser error mean?

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.

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);
}
Copy after login

Or:

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

Get error related information for analysis.

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

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

When using ajax, and During the development of related applications, programs must be written according to standards to reduce the chance of errors.

This standard includes grammar, data format, and punctuation. etc.

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

data:"{}",

ajax When processing 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 Words must be surrounded by double quotes instead of single quotes. Numeric values, Boolean types, and null are not used.

This is mainly caused by the difference between single quotes and double quotes in some old jquery versions, resulting in an error message.

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

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

Please pay attention to this function The final semicolon ";" must be added,

Otherwise, if there are multiple ajax requests on the same page, and no data is returned When issuing other ajax requests, 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.

The above is what I compiled for everyone. , I hope it will be helpful to everyone in the future. Related articles:

AJAX SpringMVC implements the paging query function of bootstrap modal box

ajax submits the mobile phone number to the database Verify and return the status value

Ajax implementation method of dynamically adding data to the drop-down list

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template