doupu3635 2012-08-31 12:50
浏览 209
已采纳

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"}

However, when I

console.log(response.orientation);

I get a response that it is undefined.

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"}

Then obj.orientation returns the correct value.

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?

  • 写回答

4条回答 默认 最新

  • duancan1732 2012-08-31 13:00
    关注

    Either put:

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接
  • ¥15 MATLAB中使用parfor,矩阵Removal的有效索引在parfor循环中受限制
  • ¥20 Win 10 LTSC 1809版本如何无损提升到20H1版本
  • ¥50 win10 LTSC 虚拟键盘不弹出
  • ¥30 微信小程序请求失败,网页能正常带锁访问
  • ¥15 Matlab求解微分方程,如何用fish2d进行预优?