Bry*_*try 2 javascript php ajax json undefined
我从AJAX调用中获取一个JSON对象并记录结果如下:
console.log(response);
Run Code Online (Sandbox Code Playgroud)
这是控制台中记录的响应:
{"filename":"new.jpg","orientation":"vertical"}
Run Code Online (Sandbox Code Playgroud)
但是,当我
console.log(response.orientation);
Run Code Online (Sandbox Code Playgroud)
我得到一个未定义的回复.
我读过的大多数答案都表明返回了一个数组而不是一个对象而且响应[0] .orientation应该可以工作,但这不是这里的情况.当我将相同的数组分配给控制台中的另一个变量时:
var obj = {"filename":"new.jpg","orientation":"vertical"}
Run Code Online (Sandbox Code Playgroud)
然后obj.orientation返回正确的值.
我在PHP中创建JSON对象:
$response=array('filename' => $newfilename, 'orientation' => $orientation);
$response=json_encode($response);
echo $response;
Run Code Online (Sandbox Code Playgroud)
显然为什么属性显示未定义?
要么:
header("Content-type: application/jason");
Run Code Online (Sandbox Code Playgroud)
在PHP中,dataType: "json"在JavaScript中指定AJAX调用,或者调用JSON.parse.
| 归档时间: |
|
| 查看次数: |
1748 次 |
| 最近记录: |