Reputation: 1592
I receive this data when I call my api
[{"DispatchNo":"xxxxx","DispatchDate":"xxxxxxx","Complete":"xxx","CustomerID":"xxxxx","Name":"xxxxxx","Contact":"","Phone":"xxxxx","ShipPhone":"xxxx","PurchaseOrder":"xxxx","OrderLoads":"5","OrderQty":"125","FreightUnitID":"x
my controoller.js code where it indicates failure is:
var jsonString = result.data.replace(/\\/g, "\\");
var orderFromApex = JSON.parse(jsonString);
orderFromApex = orderFromApex.substring(0);
console.log(orderFromApex);
//orderFromApex += '"';
orderFromApex = JSON.parse(orderFromApex);
console.log(orderFromApex);
I get error SyntaxError: Unexpected token C in JSON at position 898 at JSON.parse () I believe it is failing at orderFromApex = JSON.parse(orderFromApex). I am also console logging orderFromApex which I have posted above. What is going on? is there a problem with my JSON response?
Upvotes: 0
Views: 3323
Reputation: 2309
Your API seems to not escape characters correctly. "
for example appears unescaped inside strings closing them before they should be closed.
{"example": "Hello "World"!"}
should instead be {"example": "Hello \"World\"!"}
.
Trying to parse the first example will throw SyntaxError: Unexpected token W in JSON at position 20
. That's because the parser will look at the "
before World
and think Oh, the string is already over, what's this weird W
doing after it?.
Upvotes: 6