I'm using php json_encode to send a bunch of values to a client, where jquery
{"data":{"application":{"basics":{"email":"[email protected]","name_first":"Step","name_last":"Bob","phone":"9210938102938","occupation":"unemployed","website":"wwwcom"},"application":{"title":"Default title","guid":"9as8ua9sd8ua9sdu","language":"sv"},"job":{"title":"joburl","url":"joburl"},"letter":{"letter":"abadbabkbakbakbakbakbakbabk"},"work-experiences":[{"title":"Default WORK Experience Title","url":"wwwsscom","date_from":"1970-01-01","date_to":"1999-12-31"},{"title":"Default WORK Experience Title","url":"wwwsscom","date_from":"1970-01-01","date_to":"1999-12-31"},{"title":"Default WORK Experience Title","url":"wwwsscom","date_from":"1970-01-01","date_to":"1999-12-31"}],"educations":{"title":"Default Education","url":"ixi","date_from":"1970-01-01","date_to":"1999-12-31"},"skills":{"title":"Defailt SKILL","url":"wwwsdcom","date_from":"ixi","date_to":"ixi"},"work-samples":{"title":"A sample of my work","url":"wwwsdcom","date_from":"ixi","date_to":"1999-12-31"}}},"error":[],"warning":[]}
If I try to parse this with $.parseJSON in a script I get no object. However, if I copy/paste it directly into the console (and add ' in the beginning and end), it works. There are no error codes and I can see no linebreaks. JSON lint-tools returns no errors...
I have set the correct content type and tried the different json parsers that jquery provides.
What have I missed?
The code was cut/pasted from the jQuery tutorials. I tried some different examples but they all failed.
var jqxhr = $.getJSON( "application_controller.php", function() {
console.log( "success" );
})
.done(function() {
console.log( "second success" );
})
.fail(function() {
console.log( "error" );
})
.always(function(data) {
console.log( "complete" );
application = data;
});
// Perform other work here ...
// Set another completion function for the request above
jqxhr.complete(function() {
console.log( "second complete" );
});
});
It's true, I decoded it in the console. this is a snippet that does in in-script (it also won't work):
$.ajax({
dataType: "json",
contentType: "application/json",
url: 'application_controller.php',
data: '{id:id}',
success: function( data ) {
application = data.responseText;
application = $.parseJSON(application);/* < string */
},
fail: console.log("fail"),
complete: function(data) {
console.log(data.responseText);
application = data.responseText;
}
});