I have a WebMethod that is called by JavaScript to get data from a database. The database is off limits for this, so I can’t change anything there. I’d like to do this in JavaScript.
The string looks like this when it gets to the JavaScript:
"[{"id":"0", "value":"Avery Bradley"},{"id":"31", "value":"Jason "Jet" Terry"}]"
I’ve tried replacing ('\"', '\\\"') to add in an escape character. I got it to work on the .NET side that way Replace("\"", "\\\""). However, this is a self contained control that will be using it and that validation should be there.
What am I doing wrong?
function GetDataCallBack(childId, senderParam, senderValue, callback) {
var values;
$.ajax({
type: "POST",
async: false,
url: callback,
data: "{'id': '" + childId + "', 'parameter': '" + senderParam + "', 'value': '" + senderValue + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (json) {
values = json.d;
}
});
return values;
}
The problem is on the server side. It’s serving invalid JSON. Specifically, this inside the last object in the array:
Those quotes around
"Jet"need to be escaped. The server is giving you invalid JSON.You can try to correct broken JSON, but it will be kludgy and likely imperfect.
The server side needs to be fixed so that it serves valid JSON.