Why and when should we do JSON.parse() on jquery response ?
On some project it was needed to do JSON.parse(data) on the ajax response, and on other projects, the data was already in object form (not string). Why does it changes from time to time?
The answer is with the Content-Type returned by the server.
Content-Type: application/json
jquery will automatically parse the JSON and return directly a javascript object. However on parse error, it will throw an exception, breaking your page.
Content-Type: text/html
jquery will return plain text and you will have the responsibility of parsing the text with JSON.parse() or another library.
Recent Comments