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.

stupid
  • 21
  • 4
  • try data.data.SearchResults ...https://docs.angularjs.org/api/ng/service/$http – surya singh Sep 26 '15 at 02: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. – stupid Sep 26 '15 at 02: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 – charlietfl Sep 26 '15 at 02: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? – stupid Sep 26 '15 at 03:16
  • Maybe the `Content-type` is causing that. Are you allowed to change the response or is that an external service? – Gustavo Straube Sep 26 '15 at 04:08
  • It's an external service and yes, I believe that's the problem. Angular is reading from a .txt file and treating \n as a new line I think. – stupid Sep 26 '15 at 04:14
  • 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 – charlietfl Sep 26 '15 at 05:04

0 Answers0