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条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题