Try this to avoid this error:
myFunction(data: string) { try { JSON.parse(data); console.log(data); } catch (e) { console.log(e); } }
If you write dataType: "json"
, then jQuery will automatically parse your response into JSON before entering the "success" function. This is described in detail
in jQuery's $.ajax documentation.
Therefore, data
is already an object. You cannot pass an object to JSON.parse()
- it expects a string.
So, no need
var jso = JSON.parse(data); console.log(jso);
You can write directly
console.log(data);