Home > Web Front-end > JS Tutorial > Perfect processing of json data cannot perform success

Perfect processing of json data cannot perform success

php中世界最好的语言
Release: 2018-04-25 14:56:38
Original
2021 people have browsed it

This time I will bring you the problems of perfect processing of json data that cannot execute success, and perfect json data that cannot execute success. What are the precautions?The following is a practical case, let's take a look.

1.jquery obtains json data through the ajax method and does not execute the success callback method

Problem description: jquery obtains json data through the ajax method and does not execute the success callback method

Cause of the problem: There is a problem with the json format or it does not conform to the standard writing method, resulting in the error callback method being always executed.

Solution: Make the json format must comply with the following three standard writing methods:

 1 ) Key name: enclosed in double quotes;

2) String : enclosed in double quotes;

3) Numbers and Boolean values ​​do not need to use double quotes Brackets;

Note: Must be double brackets!

2. Ajax uses json in jQueryData typeAlways skip the success execution error statement

Execute the function

error : function(XMLHttpRequest, textStatus, errorThrown) {
//这个error函数调试时非常有用,如果解析不正确,将会弹出错误框
    alert(XMLHttpRequest.responseText); 
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus); // parser error;
}
Copy after login

and you will understand the error message; the

pop-up box displays:

<!DOCTYPE html PUBLIC "-//W3C //DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD /xhtml1-transitional.dtd">以 及<?xml version="1.0" encoding="gb2312" ?>
Copy after login

These two sentences, although I don’t know why, After I deleted it, success was successfully executed

3. When the URL in $.ajax() crosses domains, success is always not executed and an error is always entered

1) I added ServletActionContext.getResponse().setHeader("Access-Control-Allow-Origin", "*");

to solve cross-domain issues Question, you can successfully enter success

2) dataType: "json",

Remove it and see if you can enter success.

dataType is the type of information expected to be returned by the server.

Reason: The returned data type must conform to the defined data type. That is, if the dataType you define is of json type, then the returned data must be json, otherwise the program block in error will be executed.

(1) At the same time, special attention needs to be paid to whether the returned JSON data is in strict JSON format.

(2) You should also be seriously concerned when the background returns a List data (the items in the List When the data is in JSON format), if there is dirty data, it is not in strict JSON format.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

What data types can ajax handle returned by the server?

Display progress during Ajax request

The above is the detailed content of Perfect processing of json data cannot perform success. 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