How to get data from JSONP error callback in angularjs?

◇◆丶佛笑我妖孽 提交于 2019-12-11 00:57:10

问题


I am trying to get response message from jsonp error callback in angularjs, but response data is undefined. I return the response with 409 http code. If to open jsonp url directly in browser it shows "angular.callbacks._0({"message":"Reset link is incorrect"})", but I can't get this message in error callback. What am I doing wrong?

// Extend angular app for api requests
app.factory('User', function($http) {

    var User = function(data) {
        angular.extend(this, data);
    };

    // Send link for password reset to entered email
    User.reset_password = function() {
        return $http.jsonp(jsonp_url + 'user/reset_pass?_method=post&user_key=' + user_key + '&callback=JSON_CALLBACK');
    };

    return User;
});


app.controller('ResetPasswordController', function ResetPasswordController($scope, User){

    $scope.submit = function() {

        var response = User.reset_password();

        response.then(function(data){
            //Success callback
        }, function(data){
            // Error callback
            console.log(data) // Output: {config : {...}, data: undefined}
        });
    }

});

回答1:


As said Brandon Tilley it is not possible to get data from jsonp response with http error code. If you want anyways to get error message you need to send something like this {result: false, msg: 'Error occured...'} with "good" http code, for example 200.



来源:https://stackoverflow.com/questions/14517709/how-to-get-data-from-jsonp-error-callback-in-angularjs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!