javascript Angularjs $http.get 将 JSON 数据作为带有转义引号而不是 json 的字符串返回
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23257705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angularjs $http.get returning JSON data as a string with escaped quotes instead of json
提问by Frido1
Interesting problem here. I have restful backend that returns JSON. When I access the api through the browser it returns a validated json array with a json object.
有趣的问题在这里。我有返回 JSON 的宁静后端。当我通过浏览器访问 api 时,它返回一个带有 json 对象的经过验证的 json 数组。
[{"GUID_Auth":null,"Email_Address":"abc@aol,"Measure_Id":1,"Title":"Prop 41"}]
[{"GUID_Auth":null,"Email_Address":"abc@aol,"Measure_Id":1,"Title":"Prop 41"}]
but when I make a $http.get request through angularjs I instead get back a string with escaped quotes
但是当我通过 angularjs 发出 $http.get 请求时,我反而得到一个带有转义引号的字符串
got success: "[{\"GUID_Auth\":null,\"Email_Address\":\"abc@aol\",\"Measure_Id\":1,\"Title\":\"Prop 41\"}]"
获得成功:"[{\"GUID_Auth\":null,\"Email_Address\":\"abc@aol\",\"Measure_Id\":1,\"Title\":\"Prop 41\"}] ”
Here is a snippet of my angularjs controller code
这是我的 angularjs 控制器代码的片段
.controller('MainCtrl', function($scope,$http) {
$scope.GetData = function(){
var responsePromise = $http.get('http://backend.api');
responsePromise.success(function(data,status,headers,config){
console.log('got success: ' + data);
console.log('test'+ data[0].Email_Address)
});
responsePromise.error(function(data,status,headers,config){
alert('ajax failed');
});
},
This is very perplexing any help would be greatly appreciated.
这是非常令人困惑的任何帮助将不胜感激。
回答by adrichman
$http is serializing the data, so parse it before returning it
JSON.parse(data)
$http 正在序列化数据,所以在返回之前解析它
JSON.parse(data)