I made a ajax request to server and get back response from as json string.when i go for JSON.stringify, it having lots of white space in response.when i try to parse on json object received error messgae SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data.
Below is the sample code:
$.post("http://example.com/index.cfm?fuseaction=shopping.admin&stamps=getStampsRates&Order_No="+order_id+"&stamps_service_type="+selectedServiceType+"",function(data,status)
{
if(status=="success")
{
var data=JSON.parse(data);
//original resonse aspected from server
// var json ='[{"PACKAGETYPE":"Postcard","AMOUNT":0.34},{"PACKAGETYPE":"Letter","AMOUNT":0.48},{"PACKAGETYPE":"Large Envelope or Flat","AMOUNT":0.98},{"PACKAGETYPE":"Thick Envelope","AMOUNT":1.93},{"PACKAGETYPE":"Package","AMOUNT":1.93},{"PACKAGETYPE":"Large Package","AMOUNT":1.93}] ';
$.each(data, function(idx, obj) {
alert(obj.PACKAGETYPE);
});
}
});
I have made few changes then tried to parse:
var data=JSON.stringify(data);
var newJ= JSON.parse(data);
alert("newJ:"+JSON.stringify(newJ));
and getting following resonse :
"\r\n\r\n\t\r\n[{\"PACKAGETYPE\":\"Large Envelope or Flat\",\"AMOUNT\":2.69},{\"PACKAGETYPE\":\"Thick Envelope\",\"AMOUNT\":2.69},{\"PACKAGETYPE\":\"Package\",\"AMOUNT\":2.69},{\"PACKAGETYPE\":\"Large Package\",\"AMOUNT\":2.69}]\r\n\t\r\n\r\n"
And try to iterated above json object getting error TypeError: t is undefined.
Please help me to solve above problem.
Thanks
data, and show us the result (not the expected one)data = JSON.parse(JSON.parse(data))if that solves your problem, the data is doubly encoded and you should fix the server code that is generating that string