且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从json获取参数?

更新时间:2023-02-16 16:08:28

只需设置正确的数据类型json,默认值是string.

Just set proper data type json, the default one is string.

您的数据直接在data变量下!

And your data is directly under data variable!

$.post('test.php', {
    id: id
},function (data) {
    console.log(data);
    var Server = data.server;
    var Photo = data.photo;

    console.log(Server);
    console.log(Photo);

}, 'json');

另一种解决方案是在您的PHP响应中设置适当的标头:

Another solution is setting proper header in you PHP response:

Content-Type    text/javascript; charset=UTF-8

然后jQuery Intelligent Guess会自行设置正确的数据类型.

then jQuery Intelligent Guess, will set proper data type itself.