AngularJS, $http and transformResponse

前端 未结 3 1322
無奈伤痛
無奈伤痛 2020-12-08 23:36

I\'m getting a strange behaviour with AngularJS\'s $http and not really understanding how transformResponse works (the docs are a bit light on this one).

            


        
相关标签:
3条回答
  • 2020-12-08 23:47

    Try using resource query method

    https://github.com/angular/angular.js/issues/6314

    0 讨论(0)
  • 2020-12-09 00:01

    To get angular to not convert your data into an object you need to override the behavior of the default $httpProvider.defaults.transformResponse. It is actually an array of transformers. You could just set it to be empty: $http.defaults.transformResponse = []; Here is an example transformer I have used to convert 64-bit long ints to strings:

    function longsToStrings(response) {
        //console.log("transforming response");
        var numbers = /("[^"]*":\s*)(\d{15,})([,}])/g;
        var newResponse = response.replace(numbers, "$1\"$2\"$3");
        return newResponse;
    }
    

    To add a transformer to the default list, say ahead of the JSON deserializer, you can do this:

    $http.defaults.transformResponse.unshift(longsToStrings);
    
    0 讨论(0)
  • 2020-12-09 00:10

    Resource 0: "f" 1: "a" 2: "l" 3: "s" 4: "e" This finally worked for me:

       transformResponse: function (data, headersGetter) {
        return { isCorrect: angular.fromJson(data) }
        }
    
    0 讨论(0)
提交回复
热议问题