Json 错误 Laravel Ajax
Json Error In Laravel Ajax
我使用 Laravel 5.2 和 Ajax 作为 Crud
插入数据库是正确的,但是当 Laravel 响应时,浏览器在控制台中显示以下错误
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
Laravel代码:
return response()->json('ok');
Jquery代码:
$.ajax({
type: type,
url: my_url,
data: formData,
dataType: 'json',
success: function (data) {
....
error: function(data){// Error...
var errors = $.parseJSON(data.responseText); console.log(errors);
$.each(errors, function(index, value) { $.gritter.add({
title: 'Error',
text: value
});
});
}
RedyState=4
状态 = 200
对于 Json 响应,如果您尝试类似的操作会怎么样。
return response()->json(['status'=>'ok']);
您能否粘贴与此错误相关的所有控制器代码?确保在返回 json 响应之前没有回应任何内容。
例如,
Route::get('/', function () {
echo "hi";
return response()->json('ok');
});
这会导致解析错误。
你javascript代码应该是
$.ajax({
type: type,
url: my_url,
data: formData,
dataType: 'json',
success:function (xhr){
var data = xhr.data;
},
error: function (error){
}
});
为什么要使用 var errors = $.parseJSON(data.responseText);
?您的回复将始终是 json,不是吗?
我使用 Laravel 5.2 和 Ajax 作为 Crud 插入数据库是正确的,但是当 Laravel 响应时,浏览器在控制台中显示以下错误
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
Laravel代码:
return response()->json('ok');
Jquery代码:
$.ajax({
type: type,
url: my_url,
data: formData,
dataType: 'json',
success: function (data) {
....
error: function(data){// Error...
var errors = $.parseJSON(data.responseText); console.log(errors);
$.each(errors, function(index, value) { $.gritter.add({
title: 'Error',
text: value
});
});
}
RedyState=4 状态 = 200
对于 Json 响应,如果您尝试类似的操作会怎么样。
return response()->json(['status'=>'ok']);
您能否粘贴与此错误相关的所有控制器代码?确保在返回 json 响应之前没有回应任何内容。
例如,
Route::get('/', function () {
echo "hi";
return response()->json('ok');
});
这会导致解析错误。
你javascript代码应该是
$.ajax({
type: type,
url: my_url,
data: formData,
dataType: 'json',
success:function (xhr){
var data = xhr.data;
},
error: function (error){
}
});
为什么要使用 var errors = $.parseJSON(data.responseText);
?您的回复将始终是 json,不是吗?