This is probably a trivial question but I’ve been looking for the past couple of days with no solution. I am making an ajax call like this:
$.ajax({
type: 'GET',
url: "results",
dataType: 'json',
})
.fail( function (jqXHR, textStatus, errorThrown){
alert(errorThrown);
})
.done(function(data){
$.each(data.myitems, function(index, item){
// do stuff here
});
});
My json looks like this:
{
"thisvariable":1,
"anothervariable":2,
"myitems":[
{"name":"Matt",
"birthday":"1978-02-23 00:00:00"},
{"name":"Carol\y",
"birthday":"1967-05-05 00:00:00"},
{"name":"Bob",
"birthday":"1984-02-03 00:00:00"}
]
}
When I make this call, I get “SyntaxError: Unexpected token y”. It doesn’t like the backslash in Carol\y. This data is coming from a mysql database.
Whatever is creating the JSON on the server side, should be properly escaping the backslash:
Carol\\y.If you can’t control how the JSON is created, maybe you could intercept it and replace occurrences of
'\'with'\\'before it makes it back to the client.