且构网

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

AngularJS,$ http和transformResponse

更新时间:2023-01-23 18:12:05

要获得角度来不是你的数据转换成你需要覆盖默认$ httpProvider.defaults.transformResponse的行为的对象。它实际上是变压器的阵列。
你可以只将其设置为空: $ http.defaults.transformResponse = [];
下面是一个例子变压器我已经习惯了64位长整数转换为字符串:

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;
}

要变压器添加到默认列表中,说领先于JSON解串器,你可以这样做:

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

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