且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

$ http POST从服务到控制器的响应

更新时间:2023-12-04 14:29:08

$http返回承诺:

兑现诺言

updateTodoDetail: function(postDetail){
    return $http({
        method: "POST",
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        url: post_url,
        data: $.param({detail: postDetail})
    });

所以你可以做

ajaxService.updateTodoDetail(updated_details).success(function(result) {
    $scope.result = result //or whatever else.
}

或者,您可以将success函数传递给updateTodoDetail:

Alternatively you can pass the successfunction into updateTodoDetail:

updateTodoDetail: function(postDetail, callback){
    $http({
        method: "POST",
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        url: post_url,
        data: $.param({detail: postDetail})
    })
    .success(callback);

您的控制器有

ajaxService.updateTodoDetail(updated_details, function(result) {
    $scope.result = result //or whatever else.
})

我希望使用第一个选项,这样我也可以在不传递那些函数的情况下处理错误等.

I would prefer the first option so I could handle errors etc as well without passing in those functions too.

(注意:我尚未测试上面的代码,因此可能需要进行一些修改)

(NB: I haven't tested the code above so it might require some modification)