Angular 1.3.0 distinguishes JSON responses, how do I override this or pre-parse the response before Angular?

In the latest version of Angular (v1.3.0), they added a fix for the content header for the / json application. Now all my answers get an error because they are invalid JSON. I know that I need to change the back-end for the response using a regular text header, but I can’t control it at the moment. Is there any way to pre-analyze the response before Angular tries to parse it?

I think this was the fix they made: https://github.com/angular/angular.js/commit/7b6c1d08aceba6704a40302f373400aed9ed0e0b

The problem is that the answer that I get from the frontend has a security prefix that does not match the one that Angular checks.

I tried to add the http interceptor to the config, but this did not help, it still parses it after Angular.

$httpProvider.interceptors.push('parsePrefixedJson');

The error I get in my console (it comes from deserializing a JSON string in Angular):

SyntaxError: Unexpected token w
at Object.parse (native)
at fromJson ...
+4
source share
2 answers

You have to use

$http.defaults.transformResponse

You also do not want to use .push(). You will need your transformer to do your job in front of the default angular transformers. Use instead .unshift().

So your code should be

$http.defaults.transformResponse.unshift(transformResponse);

transformResponse - , JSON.

+1

, Angular:

app.run(['$http',
    function($http) {
        var parseResponse = function(response) {
            // parse response here

            return response;
        };

        $http.defaults.transformResponse = [parseResponse];
    }
]);

, , .

/: AngularJS, $http transformResponse

+1

All Articles