Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.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 AngularJS$http结果与承诺_Javascript_Angularjs_Asynchronous_Q_Angular Promise - Fatal编程技术网

Javascript AngularJS$http结果与承诺

Javascript AngularJS$http结果与承诺,javascript,angularjs,asynchronous,q,angular-promise,Javascript,Angularjs,Asynchronous,Q,Angular Promise,我是angular$q服务的新手。我正在使用$http和angular$q服务来实现异步请求。下面是我的代码,我无法得到后端api的结果。(json) Services.js: .service('httpService', function($q, $http, $timeout) { var asyncRequest = function(url) { return $http.get(url) .then(function(response) {

我是angular$q服务的新手。我正在使用
$http
和angular
$q
服务来实现异步请求。下面是我的代码,我无法得到后端api的结果。(json)

Services.js

.service('httpService', function($q, $http, $timeout) {

 var asyncRequest = function(url) {
    return $http.get(url)
        .then(function(response) {
            //res is the index of an array in php, which will be encoded.
            return response.res;

        }, function(response) {
            // something went wrong
            return $q.reject(response.res);
        });
 };
 return {
   asyncRequest : asyncRequest 
 };

});
var result = httpService.test(url)
.then(function(data) {
    // Line below gives me "undefined"
    console.log(data);
}, function(error) {
    alert("Error...!");
});
Controller.js

.service('httpService', function($q, $http, $timeout) {

 var asyncRequest = function(url) {
    return $http.get(url)
        .then(function(response) {
            //res is the index of an array in php, which will be encoded.
            return response.res;

        }, function(response) {
            // something went wrong
            return $q.reject(response.res);
        });
 };
 return {
   asyncRequest : asyncRequest 
 };

});
var result = httpService.test(url)
.then(function(data) {
    // Line below gives me "undefined"
    console.log(data);
}, function(error) {
    alert("Error...!");
});
上面提到的行,给了我未定义的。(当然,我可以在main函数中编写console.log(data),但这不是一个好的做法,因为我想将结果返回给controller

关于我实施的
$q
服务,有没有更简单的方法


任何想法都将不胜感激。

在这种情况下,您应该不要使用
$q
,因为
$http
已经返回了一个承诺。使用两个在一起,效率低。(
$q
用于使用非角度异步函数,如地理查找)

Services.js:

.service('httpService', function($http, $timeout) {

  var asyncRequest = function(url) {
    return $http.get(url)
  };
  return {
   asyncRequest : asyncRequest 
  };

});
Controller.js:

var result = httpService.asyncRequest(url)
.then(function(res) {
    console.log(res.data);
}, function(error) {
    alert("Error...!");
});

您应该像这样回报您的对象的承诺

首先,您使用的是工厂风格而不是服务。服务只是一个函数,其中方法定义在此引用上

我认为您不需要使用
。那么
在服务中只需返回$http返回的承诺即可

app.service('httpService', function($q, $http, $timeout) {

  this.asyncRequest = function(url) {
    return $http.get(url);
  };
});
和在控制器中

 var result = httpService.test(url)
  .then(function(res) {
    // Line below gives me "undefined"
    console.log(res.data);
  }, function(error) {
    alert("Error...!");
  });

我认为您在服务中使用的是at-factory的语法

.service('httpService', function($q, $http, $timeout) {
   this.asyncRequest = function(url) {};
});


上述行中的响应已被拒绝。你不需要拒绝其他任何东西。所以你不需要
$q

首先,你已经回报了一个承诺。您可以在控制器中通过添加
success()
error()
代理
$http
承诺来处理它。 第二,这是异步操作。并且不能从成功回调返回响应,如
jQuery.ajax()
。这不是同步调用,这是异步调用,您必须使用回调。你的错误在这里。当响应被解决或拒绝时,只需返回承诺并在控制器中处理它

因此,控制器代码可以如下所示:

httpService.asyncRequest({
    ...
}).success(function(successfulResponse) {
    ...
}).error(function(failedResponse) {
    ...
});

它应该是
response.data
,而不是
response.res
。如果
res
是服务器返回的响应数据对象的索引,则只需返回
response.data.res
@ryeballar是,您的注释和Simon的答案相结合就可以得到正确的解决方案。谢谢大家,非常感谢。还有一个问题。如何使其嵌套?我的意思是另一个
$http
取决于第一个的结果。@Bergi顺便说一句,我没有复制你的答案。我在我的项目中的许多项目中都使用了类似的服务。还有一件事,我的回答和你们在链接中提到的不同。我的代码中没有任何catch块。无论如何,我尊重社区和所有像你这样受人尊敬的成员。所以,如果我的答案被你们否决,就没有问题了。答案的复印件?不,我想说的是“你不应该回报像这样的对象的承诺”。在这里使用是可以的,但没有什么区别,它仍然是延迟的反模式。顺便说一句,我甚至没有投反对票,是另外5个人,其中一些人似乎同意我的观点。