I am facing an issue with passing a JSON string in an ajax request. Here is the code snippet:
NewOrder = JSON.stringify (NewOrder);
alert (NewOrder);
var req = {
url: '/cgi-bin/PlaceOrder.pl',
method: 'POST',
headers: { 'Content-Type': 'application/json'},
data: "mydata="+ NewOrder
};
$http(req)
.success(function (data, status, headers, config) {
alert ('success');
})
.error(function (data, status, headers, config) {
alert (status);
alert (data);
alert ('Error')
});
The alert (NewOrder) displays:
{"ItemList":[{"ItemName":"Quality Plus Pure Besan 500 GM","Quantity":1,"MRP":"28.00","SellPrice":"25.00"}],"CustomerID":1,"DeliverySlot":2,"PaymentMode":1}
This appears to be a valid JSON string.
However, I encountered an error on the server side while trying to decode the JSON string:
my $decdata = decode_json($cgi->param('mydata'));
The error message received was: malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "(end of string)")
If anyone could help me understand why this error is occurring, it would be greatly appreciated.