Home  >  Article  >  Web Front-end  >  jquery.post usage supplement about type setting issues_jquery

jquery.post usage supplement about type setting issues_jquery

WBOY
WBOYOriginal
2016-05-16 17:05:321110browse

jquery.post usage http://www.jb51.net/article/45181.htm
When using ajax to obtain data data, you can get it directly from data.foo. 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 ")");

Reference: The relationship between javascript eval and JSON

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 Then there is no such problem, and the returned object is json object
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