使用$ httpBackend。然后保证有效,但.success回调不起作用

时间:2015-10-21 13:44:46

标签: javascript angularjs httpbackend

我的测试文件中有以下后端定义:

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

在我的控制器中,我尝试了两个请求:

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

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

正如我在评论中所指出的,' .then'承诺捕获虚假的回应,同时“成功”#39;回调没有(我没有错误,但调试器甚至没有进入回调的闭包。

任何想法为什么?

谢谢:)

1 个答案:

答案 0 :(得分:1)

.success将结果对象向上扩展,因此第一个参数是数据而不是响应对象。因此,以下更改应适用于您的电话:

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

另外,如果你链接了promises,.success会返回原始的HttpPromise($ http.post的结果)而不是新的promise,如果你在.success函数中返回一些内容。