1

My Angular get request fails when the JSON data I retrieve contains forward slashes in the data. I'm not sure how to handle such data as specially as I am using $http.get.

Here is the related code:

$http.get(APIroute).
success(function(data, status, headers, config) {
  console.log(data);
  vm.results = data.SearchResults;
}).
error(function(data, status, headers, config) {
  console.log(data);
});

Data with error (retrieved via other means, not angular):

{
  "SearchResults": [
    {
      "PageCount": "1"
    },
    {
      "SEARCHVAL": "ABC Brickworks Food Centre"
    },
    {
      "SEARCHVAL": "ABC Brickworks Market & Food Centre"
    },
    {
      "SEARCHVAL": "ADAM ROAD FOOD CENTRE,\r\n2 ADAM ROAD,\r\nSINGAPORE 283876"
    },
    {
      "SEARCHVAL": "AMOY STREET FOOD CENTRE,\r\nTELOK AYER STREET,\r\nSINGAPORE 069111"
    }
  ]
}

Response Headers:

Access-Control-Allow-Origin:*
Access-Control-Request-Method:GET
Cache-Control:max-age=43200
Connection:keep-alive
Content-disposition:inline; filename=onemap.txt
Content-Encoding:gzip
Content-Type:text/plain
Date:Sat, 26 Sep 2015 03:11:42 GMT
Keep-Alive:timeout=20
Server:nginx
Transfer-Encoding:chunked
Vary:Accept-Encoding

console error log:

SyntaxError: Unexpected token 
    at Object.parse (native)
    at uc (file:///Users/kelvinkoh/Work/SoftEng/lib/js/angular.min.js:15:480)
    at Zb (file:///Users/kelvinkoh/Work/SoftEng/lib/js/angular.min.js:82:229)
    at file:///Users/kelvinkoh/Work/SoftEng/lib/js/angular.min.js:83:143
    at m (file:///Users/kelvinkoh/Work/SoftEng/lib/js/angular.min.js:7:322)
    at cd (file:///Users/kelvinkoh/Work/SoftEng/lib/js/angular.min.js:83:125)
    at d (file:///Users/kelvinkoh/Work/SoftEng/lib/js/angular.min.js:84:380)
    at file:///Users/kelvinkoh/Work/SoftEng/lib/js/angular.min.js:118:334
    at n.a.$get.n.$eval (file:///Users/kelvinkoh/Work/SoftEng/lib/js/angular.min.js:132:452)
    at n.a.$get.n.$digest (file:///Users/kelvinkoh/Work/SoftEng/lib/js/angular.min.js:129:463)

Anyone have any idea on how to handle such errors? Any insights would be appreciated. Thanks in advance.

7
  • try data.data.SearchResults ...docs.angularjs.org/api/ng/service/$http Commented Sep 26, 2015 at 2:28
  • That doesn't work. Do note that this code works ok when the data retrieved does not contain any forward slashes. Changing to data.data.SearchResults gives error on the same parsing error on all requests. Commented Sep 26, 2015 at 2:37
  • is the data being sent as jsonp? Any extra characters outside of the {} in json? Copy the actual response from dev tools newtork and run it through jsonlint.com Commented Sep 26, 2015 at 2:44
  • I've updated the question to include the response headers. The data is valid JSON, but on raw the \n processes to a new line. Is the Content-Type:text/plain tripping angular to process \n as a new line, breaking the json? Commented Sep 26, 2015 at 3:16
  • 1
    2 things I can think of...a proxy on your server that changes the contentType to application/json or $httpInterceptor that removes the line breaks Commented Sep 26, 2015 at 5:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.