Ok people. I got this error and i tried all the solutions people with the same problem used to solve it. Nothing. I need to use jQuery "ajax" function to get some data from a php and then distribute the data in some inputs. If i use 'text' as dataType it works like a charm, but with the json dataType i get this error:
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
My code is:
js
$.ajax({
type: 'get',
url: globals.rootdir + '/controller.php?op=do_something&module=56',
dataType: 'json',
data: {
year: previouslyDeclaredYearVar
},
success: function (data) {
alert('good');
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
php - the controller do_something function
if (!isAjaxRequest()){
return;
}
header("Content-type: application/json");
... do things
echo json_encode(['result'=>'test-ajax-ok']);