JSON对象属性未定义 [英] JSON object properties are undefined

查看:151
本文介绍了JSON对象属性未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从AJAX调用中获取一个JSON对象,并记录如下结果:

I'm getting a JSON object back from an AJAX call and logging the result like this:

console.log(response);

这是控制台中记录的响应:

And this is the response logged in the console:

{"filename":"new.jpg","orientation":"vertical"}

但是,当我

console.log(response.orientation);

我得到一个未定义的答复.

I get a response that it is undefined.

我读过的大多数答案都表明,返回的是数组而不是对象,并且response [0] .orientation应该起作用,但实际情况并非如此.当我将相同的数组分配给控制台中的另一个变量时:

Most of the answers I've read indicate that an array was returned instead of an object and that response[0].orientation should work, but that is not the case here. When I assign the same array to another variable in the console:

var obj = {"filename":"new.jpg","orientation":"vertical"}

然后obj.orientation返回正确的值.

Then obj.orientation returns the correct value.

我正在用PHP创建JSON对象:

I'm creating the JSON object in PHP:

$response=array('filename' => $newfilename, 'orientation' => $orientation);
$response=json_encode($response);
echo $response;

显而易见,为什么这些属性显示为未定义吗?

Is it apparent why the properties are showing undefined?

推荐答案

可以放:

header("Content-type: application/jason");

在PHP中,在JavaScript的AJAX调用中指定dataType: "json",或调用JSON.parse.

in the PHP, specify dataType: "json" in the AJAX call in the JavaScript, or call JSON.parse.

这篇关于JSON对象属性未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆