简体   繁体   English

与$ httpBackend .then承诺工作,但.success回调不起作用

[英]With $httpBackend .then promise works but .success callback doesn't

I have the following backend definition in my test file: 我的测试文件中具有以下后端定义:

authRequestHandler = $httpBackend.whenPOST(my_request_url)
.respond(
{
    userId: 'panda',
    token: 'panda_token'
});

And in my controller I tried both requests: 在我的控制器中,我尝试了两个请求:

$http.post(SignUpUrl)
.success(function (results, status, headers, config) { //this doesn't work
    $scope.data = results.data;
});

And

$http.post(SignUpUrl)
.then(function (results) {   //this works
    $scope.data = results.data;
});

As I noted in the comments, the '.then' promise captures the fake response, while the '.success' callback does not (I get no errors but the debugger doesn't even enter the closure of the callback. 正如我在评论中指出的那样,“。then”承诺捕获了虚假响应,而“ .success”回调没有捕获(我没有收到任何错误,但调试器甚至没有进入回调的结束符。

Any ideas why? 有什么想法吗?

Thanks :) 谢谢 :)

.success spreads the result object up, so the first parameter is the data and not the response object. .success将结果对象向上扩展,因此第一个参数是数据而不是响应对象。 So the following change should work for your call: 因此,以下更改应适用于您的通话:

$http.post(SignUpUrl)
.success(function (data, status, headers, config) { //this doesn't work
    $scope.data = data;
});

Also, if you are chaining promises, .success returns the original HttpPromise (result of $http.post) instead of a new promise if you return something in the .success function. 另外,如果要链接诺言,.success将返回原始HttpPromise($ http.post的结果),而不是新诺言(如果在.success函数中返回内容)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM