【发布时间】:2017-03-02 07:32:01
【问题描述】:
我只是调用返回 josn 编码数据的 api 并尝试打印对象属性但显示未定义但是当我打印该对象时,该对象具有该属性和值。
我的代码
function sendData(postData, url){
var response = apiCall(postData,url);
console.log(response.email)
console.log(response.count);
}
function apiCall(postData, postUrl){
var response = {};
$http({
method : 'POST',
url : postUrl,
data : postData,
headers : {'Content-Type': 'application/json'}
}).success(function(data) {
console.log(data)
for (var attr in data) {
if (data.hasOwnProperty(attr)) response[attr] = data[attr];
}
});
return response;
}
基于php的api
<?php
$_POST = json_decode(file_get_contents('php://input'), true);
$response = array();
$response['email'] = $_POST['oauth']['email'];
$response['type'] = $_POST['oauth']['type'];
echo json_encode($response);
?>
在控制台中响应数据
对象{电子邮件:“sameerdighe14@gmail.com”,类型:“google”}
【问题讨论】:
-
请添加您的回复数据。
-
当你已经在做
(var attr in data)时,你真的需要这条线if (data.hasOwnProperty(attr))吗? -
@brk 检查属性,如果有属性为空,则不会将其分配给其他对象。
-
我添加了响应数据@lin
标签: javascript php angularjs