Note that there are some explanatory texts on larger screens.

plurals
  1. POAngularJS http.get verify valid json
    primarykey
    data
    text
    <p>When getting a json from a URL I only want to work with it, when the data is valid.</p> <p>my approach so far by using <a href="https://github.com/douglascrockford/JSON-js" rel="nofollow">JSON</a>:</p> <pre><code>$http.get( 'data/mydata.json' + "?rand=" + Math.random() * 10000, {cache: false} ) .then(function (result) { try { var jsonObject = JSON.parse(JSON.stringify(result.data)); // verify that json is valid console.log(jsonObject) } catch (e) { console.log(e) // gets called when parse didn't work } }) </code></pre> <p>However before I can do the parsing, angular already fails itself</p> <blockquote> <p>SyntaxError: Unexpected token { at Object.parse (native) at fromJson (<a href="http://code.angularjs.org/1.2.0-rc.2/angular.js:908:14" rel="nofollow">http://code.angularjs.org/1.2.0-rc.2/angular.js:908:14</a>) at $HttpProvider.defaults.defaults.transformResponse (<a href="http://code.angularjs.org/1.2.0-rc.2/angular.js:5735:18" rel="nofollow">http://code.angularjs.org/1.2.0-rc.2/angular.js:5735:18</a>) at <a href="http://code.angularjs.org/1.2.0-rc.2/angular.js:5710:12" rel="nofollow">http://code.angularjs.org/1.2.0-rc.2/angular.js:5710:12</a> at Array.forEach (native) at forEach (<a href="http://code.angularjs.org/1.2.0-rc.2/angular.js:224:11" rel="nofollow">http://code.angularjs.org/1.2.0-rc.2/angular.js:224:11</a>) at transformData (<a href="http://code.angularjs.org/1.2.0-rc.2/angular.js:5709:3" rel="nofollow">http://code.angularjs.org/1.2.0-rc.2/angular.js:5709:3</a>) at transformResponse (<a href="http://code.angularjs.org/1.2.0-rc.2/angular.js:6328:17" rel="nofollow">http://code.angularjs.org/1.2.0-rc.2/angular.js:6328:17</a>) at wrappedCallback (<a href="http://code.angularjs.org/1.2.0-rc.2/angular.js:9106:81" rel="nofollow">http://code.angularjs.org/1.2.0-rc.2/angular.js:9106:81</a>) at <a href="http://code.angularjs.org/1.2.0-rc.2/angular.js:9192:26" rel="nofollow">http://code.angularjs.org/1.2.0-rc.2/angular.js:9192:26</a> angular.js:7861</p> </blockquote> <p>How can I prevent angular from throwing this error or how else should I handle verifying the JSON ?</p> <p><strong>UPDATE: Solution:</strong></p> <pre><code>$http.get( // url: 'data/mydata.json' + "?rand=" + Math.random() * 10000 , // config: { cache: false, transformResponse: function (data, headersGetter) { try { var jsonObject = JSON.parse(data); // verify that json is valid return jsonObject; } catch (e) { console.log("did not receive a valid Json: " + e) } return {}; } } ) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload