I have a web service which returns the following Response/Payload:
[{"msg":"Order requires backorder","status":"ERROR"}]
I have the following AJAX code to process this result:
$.post("/myorder/{{ order_id }}",
{},
function(data, status){
var dataJson = $.parseJSON(data);
if (status=="success") {
if (dataJson['status'] == "OK") {
alert('Success');
} else {
alert(dataJson['msg']);
}
} else {
alert("ERROR. Data: " + data + "\nStatus: " + status);
}
}
);
Note: {{ order_id }}
comes from used template system and it is not part of the question.
I get the following error in the console:
SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data
I have also tried not using .parseJSON
and using directly variable data
without sucess.
Is .post
the right method to use? How could I further troubleshoot or what shall be done to process this JSON payload?
First you do not need parseJSON since jQuery will parse it. Second you write your service returns an array with one object in there so using data.status would return undefined. But using data[0].status and data[0].msg would work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.