Uncaught SyntaxError: Unexpected token < in JSON at position 0
I need to return json, send it throw $.get request and show it with .html funtion. I would prefer dom option, but i have a laravel framework on backend, and i'm still using blade. So i have something like:
$response = [
'success' => true,
'content' => view('/collette/create_step_01', compact('prodotto', 'destinatario', 'input'))->render(),
];
and i return
return json_encode($response);
There is a better way for return json on laravel?
On js i have
var qs = getQueryString();
if (typeof(step) === 'undefined') {
initColletta(); //define step
}
$.get(url + 'collette/create/steps/' + step, qs, function(data) {
var res = JSON.parse(data);
if (res.success) {
showModalBody(res.content);
if (typeof(res.stop) !== 'undefined' && res.stop) {
$('#step-next').attr('href', res.link).unbind('click');
}
}
});
To return Json in Laravel you should use
return response()->json(your_json_here);
And as far as the error is concerned that is likely due to the fact that you don't need to Parse the JSON as it is already a valid JSON object.
The reason it says Uncaught SyntaxError: Unexpected token < in JSON at position 0
is because there is actually an error from the response itself...
Which i will assume to be the first "<" in the view you are returning via json_encode();
I'll suggest you confirm what is being returned first... probably via var_dump()
.
And of course, another way to return json via laravel like you asked is
return response()->json(json_array);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.