Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
访问javascript中的对象属性时出现问题显示未定义?_Javascript_Php_Angularjs - Fatal编程技术网

访问javascript中的对象属性时出现问题显示未定义?

访问javascript中的对象属性时出现问题显示未定义?,javascript,php,angularjs,Javascript,Php,Angularjs,我只是调用api,它返回josn编码的数据,并试图打印对象属性,但显示未定义,但当我打印该对象时,该对象具有该属性和值 我的代码 function sendData(postData, url){ var response = apiCall(postData,url); console.log(response.email) console.log(response.count); } function apiCall(pos

我只是调用api,它返回josn编码的数据,并试图打印对象属性,但显示未定义,但当我打印该对象时,该对象具有该属性和值

我的代码

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);
?> 

控制台中的响应数据

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;   
    }
对象{电子邮件:“sameerdighe14@gmail.com,键入:“google”}


你需要用承诺来实现它。一旦HTTP请求成功完成,您的
success
函数就被称为async。这样
返回响应在请求完成之前执行->因此它仍然是一个空对象
{}
。用来使它工作。这是一个简单的工作原理


你需要用承诺来实现它。一旦HTTP请求成功完成,您的
success
函数就被称为async。这样
返回响应在请求完成之前执行->因此它仍然是一个空对象
{}
。用来使它工作。这是一个简单的工作原理


代码没有按预期的顺序运行<代码>$http
运行需要时间,因此
apiCall
在修改之前返回响应

要解决这个问题,您需要使用一个承诺来确保您的代码只在您拥有所需的所有数据时运行

此外,从
$http
返回的数据有一个属性
data
,其中包含调用的结果

function sendData(postData, url) {
  // subscribe to promise, and add your code in the then block
  apiCall(postData,url)
    .then(function(response) {
      console.log(response.email);
      console.log(response.count);
    });
}

function apiCall(postData, postUrl) {
  // return the promise
  return $http({
    method  : 'POST',
    url     : postUrl,
    data    : postData, 
    headers : {'Content-Type': 'application/json'}
  }).success(function(response) { 
    var result = {};
    for (var attr in response.data) {
      if (response.data.hasOwnProperty(attr)) {
        result[attr] = response.data[attr];
      }
    }  
    return result;   
  });
}

请注意,$http请求中的
data
属性保证是一个普通对象,其原型上没有额外的属性,因此实际上不需要检查
hasOwnProperty
。所有这些都可以简化:

function apiCall(postData, postUrl) {
  // return the promise
  return $http({
    method  : 'POST',
    url     : postUrl,
    data    : postData, 
    headers : {'Content-Type': 'application/json'}
  }).success(function(response) { 
    return response.data;
  });
}

代码没有按预期的顺序运行<代码>$http
运行需要时间,因此
apiCall
在修改之前返回响应

要解决这个问题,您需要使用一个承诺来确保您的代码只在您拥有所需的所有数据时运行

此外,从
$http
返回的数据有一个属性
data
,其中包含调用的结果

function sendData(postData, url) {
  // subscribe to promise, and add your code in the then block
  apiCall(postData,url)
    .then(function(response) {
      console.log(response.email);
      console.log(response.count);
    });
}

function apiCall(postData, postUrl) {
  // return the promise
  return $http({
    method  : 'POST',
    url     : postUrl,
    data    : postData, 
    headers : {'Content-Type': 'application/json'}
  }).success(function(response) { 
    var result = {};
    for (var attr in response.data) {
      if (response.data.hasOwnProperty(attr)) {
        result[attr] = response.data[attr];
      }
    }  
    return result;   
  });
}

请注意,$http请求中的
data
属性保证是一个普通对象,其原型上没有额外的属性,因此实际上不需要检查
hasOwnProperty
。所有这些都可以简化:

function apiCall(postData, postUrl) {
  // return the promise
  return $http({
    method  : 'POST',
    url     : postUrl,
    data    : postData, 
    headers : {'Content-Type': 'application/json'}
  }).success(function(response) { 
    return response.data;
  });
}


请添加您的响应数据。当您已经在执行
(数据中的var attr)时,是否确实需要这一行
if(data.hasOwnProperty(attr))
?@brk它会检查属性,如果有带有空字段的属性,则不会将其分配给其他对象。我在@lin添加了一个响应数据。请添加您的响应数据。如果(data.hasOwnProperty(attr)),当您已经在执行
(数据中的var attr)时,您真的需要这一行吗
?@brk它会检查属性,如果有带有空字段的属性,则不会将其分配给其他对象。我添加了一个响应数据@linI仍然存在相同的问题。控制台中的打印未定义HH对我有效。但计数仍然无效@lin@SaMeEr计数什么?console.log(filteredData.Count);这句话,我还是有同样的问题。控制台中的打印未定义HH对我有效。但计数仍然无效@lin@SaMeEr计数什么?console.log(filteredData.Count);这句话。这行不通<代码>然后(函数(响应)保存的是
$http
的原始回调数据,而不是成功部分返回的数据。
success
返回一个承诺。因此,任何修改都会被保留。来源:是的,但这个承诺是为了处理成功承诺本身。请检查并提供一些反馈,meThis wil也对这个讨论感兴趣l不工作。
。然后(功能(响应)
保存的是
$http
的原始回调数据,而不是成功部分返回的数据。
success
返回一个承诺。因此,任何修改都会被保留。来源:是的,但这个承诺是为了处理成功承诺本身。请检查并提供一些反馈,这个讨论对我来说也很有趣