Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
110
rated 0 times [  117] [ 7]  / answers: 1 / hits: 34124  / 11 Years ago, thu, october 10, 2013, 12:00:00

Problem



I'm getting a parse error on some of my json data, because it includes single quotes.
For example, some of my data could look like this:



Larry's data



I've read the following article:
jQuery single quote in JSON response



and I've been trying to implement some of the solutions but I haven't been able to get rid of my parse error.



Code



In my model, I'm using a lua library to encode my data as json.
The model returns data that looks like this:



[{createddatetime:2013-09-10 17:56:55,description:John Doe's phone,number:72051,createdname:conversion script,user:23123,position:46,id:49,user_id:822,password:rwer234}]


In my view, my code currently looks like this:



  $.ajax({
url:myurl + '?startpos=' + page_index * items_per_page + '&numberofrecordstograb=' + items_per_page + '&viewtype=json',

success: function(data){

console.log('inside');
for(var i=0;i<data.length;i++) {
var deviceobj = data[i];
newcontent = newcontent + <TR>;
newcontent=newcontent + '<TD>';

//add EDIT hyperlink
if ($(#editdevicesettings).val() == true) {
var temp = $(#editlinkpath).val();
newcontent=newcontent + temp.replace(xxx,deviceobj[device_id]) + '&nbsp;&nbsp;';
}

//add DELETE hyperlink
if ($(#deletedevice).val() == true) {
var temp = $(#deletelinkpath).val();
newcontent=newcontent + temp.replace(xxx,deviceobj[device_id]);
}
newcontent=newcontent + '</TD>';

newcontent=newcontent + '<TD>' + deviceobj[number] +'</TD>';
newcontent=newcontent + '<<TD>' + deviceobj[user] + '</TD>';
newcontent=newcontent + '<<TD>' + deviceobj[password] + '</TD>';
if (deviceobj[name]) {
newcontent=newcontent + '<TD>' + deviceobj[name] + '</TD>';
}
else {
newcontent=newcontent + '<TD>&nbsp;</TD>';
}
newcontent=newcontent + '<TD>' + unescape(deviceobj[description]) + '</TD>';
newcontent = newcontent + </TR>;
}// end for
// Replace old content with new content
$('#Searchresult').html(newcontent);
}//end if

},
error: function(request, textStatus, errorThrown) {
console.log(textStatus);
console.log('========');
console.log(request);

},
complete: function(request, textStatus) { //for additional info
//alert(request.responseText);
console.log(textStatus);
}
});


But I still get the parse error on this particular record.



Any suggestions would be appreciated.
Thanks.



EDIT 1



I've changed my logic so that when it fails, it print out request.responseText into the console. Here's what it looks like:



[{createddatetime:2013-09-10 17:56:55,description:John Doe's phone,number:72051,createdname:conversion script,user:28567,position:46,id:49,user_id:822,password:rwer234}]


The apostrophe is still escaped.



EDIT 2



Here's what my code looks like on the server side (aka. in the model):



get_device_records = function(ajaxdata)
local results = list_devices(nil,false,ajaxdata.startpos, ajaxdata.numberofrecordstograb)
return results.value
end

More From » jquery

 Answers
13

Looks like you are doing double serialisation on the server side. Happens for example if your web framework automatically serialises the returned object but you make an extra explicit, unnecessary serialise call on your object. The fact that your code works with no ' cases proves this: jquery makes one parse automatically (because of the dataType) then you run another parseJSON. That shouldn't work :)
Fix your serialisation on the server side and remove the unnecessary parseJSON call from your success method. Every other solution is just a workaround not a real fix.


[#75081] Thursday, October 10, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sonja

Total Points: 541
Total Questions: 113
Total Answers: 114

Location: Anguilla
Member since Sun, Jan 29, 2023
1 Year ago
sonja questions
Mon, Nov 30, 20, 00:00, 4 Years ago
Sun, Oct 11, 20, 00:00, 4 Years ago
Thu, May 21, 20, 00:00, 4 Years ago
Sun, Nov 10, 19, 00:00, 5 Years ago
Mon, Aug 26, 19, 00:00, 5 Years ago
;