Home >Web Front-end >JS Tutorial >jquery.post usage type setting problem_jquery

jquery.post usage type setting problem_jquery

WBOY
WBOYOriginal
2016-05-16 16:58:22993browse

When using ajax to obtain data, data.foo can be obtained directly. However, lower versions of jquery will not work. For example, before 1.4

Copy code The code is as follows:

$ .post('/admin/UserBookView.do', {}, function(data) {
console.info(data);
});

Print data information and display is a string in json format, as follows:
Copy code The code is as follows:

{" acceptIs":null,"entity":null,"refuseIs":null,"result":{"pageSize":10,
"resultList":[{"PRICE":3,"WCTIME":null," NOTE":"Points exception","CKTIME":null,"CUSER":"admin",
"CTIME":"2013/12/30 17:03:16","PHONE":"13111050937", "ADDR":"Test Address","CUSERID":"1","SLTIME":null}],
"resultListArray":null,"titles":["ID","CTIME","STATE" "PRICE","NOTE"],"totalPage":1,"totalSize":4},
"source":null,"storageIs":null,"treeNodes":null}

If type is not set, the data returned by default is text type

When we use data. to try to get the value inside, undefined is returned

There are two solutions at this time :

1: Use the eval function to convert the json string into a json object
Copy the code The code is as follows:

var datas=eval("(" data ")");

Two: Specify type
Copy code The code is as follows:

$.post('/admin/UserBookView.do', {}, function(data) {
console. info(data);
},"json");

Higher versions such as 1.8 do not have this problem and return json objects.
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