AngularJS service not invoking error callback on save() method

前端 未结 6 2158
伪装坚强ぢ
伪装坚强ぢ 2021-01-17 18:07

So I\'m using angularjs restful service $resource and I\'m calling $save function. However, the error callback I pass to it is not being called. The server is sending a 41

6条回答
  •  轮回少年
    2021-01-17 18:39

    I'm copying from the ngResource documentation:

    The action methods on the class object or instance object can be invoked with the following parameters:

    • HTTP GET "class" actions: Resource.action([parameters], [success], [error])
    • non-GET "class" actions: Resource.action([parameters], postData, [success], [error])
    • non-GET instance actions: instance.$action([parameters], [success], [error])

    Success callback is called with (value, responseHeaders) arguments. Error callback is called with (httpResponse) argument.

    $save is considered as a non-GET "class" action, so you must to use an extra postData parameter. Using the same question example, this should work:

    modalScope.submit = function(user) {
        user.$save( {}, {}, function(data,headers) {
            // do the success case
        }, function(response) {
            // do the error case                
        });
    };
    

    Keep an eye on the error callback, compared with the example, is calling with just one argument, which brings all the http response.

提交回复
热议问题