Frido1
Frido1

Reputation: 189

Angularjs $http.get returning JSON data as a string with escaped quotes instead of json

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.

[{"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

got success: "[{\"GUID_Auth\":null,\"Email_Address\":\"abc@aol\",\"Measure_Id\":1,\"Title\":\"Prop 41\"}]"

Here is a snippet of my angularjs controller code

.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.

Upvotes: 8

Views: 11575

Answers (1)

adrichman
adrichman

Reputation: 1235

$http is serializing the data, so parse it before returning it JSON.parse(data)

Upvotes: 12

Related Questions