I need to save a json object to a file. Therefore I'm using another PHP script which writes the data to a file. Data and filename are sent by an ajax POST request.
var results = $('results');
var filename = $('#filename').val();
$.ajax({
url: 'jsonWriter.php',
type: 'POST',
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify([{data: data, filename: filename }]),
success: function(data) {
results.html(data);
},
error: function(jqXhr, textStatus, error) {
results.html("<p class=\"error\">ERROR: " + textStatus + ", " + error + "</p>");
}
});
When I monitor the request in Fiddler, it looks fine and the posted data is correct. However, somehow the error function is invoked. It outputs
ERROR: parsererror, SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data
I copied the posted JSON data from Fiddler into different online parsers, which ensured it is valid.
Why is the error function invoked anyway?