Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 278
Frido1 Avatar asked Apr 24 '14 00:04

Frido1


1 Answers

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

like image 133
adrichman Avatar answered Sep 19 '22 07:09

adrichman