laravel 5.8 响应 ajax 请求时出现 500 内部服务器错误
laravel 5.8 responding with 500 internal server error on ajax request
我正在尝试发送一个 array
到我的控制器以保存在数据库中,如果在这种情况下我制作 array a string
它将保存到数据库中。
所以当 data: {'mapData' : arrayAllDrawings},
它崩溃时(arrayAllDrawings 是一个数组)。但是当它像这样 data: {'mapData' : 'string'},
这样的字符串被硬编码时,它会通过 success message
保存在数据库中
当服务器抛出 500 内部服务器错误时来自 storage/logs
的错误日志
[2019-03-28 08:40:09] local.ERROR: Array to string conversion {"userId":1,"exception":"[object] (ErrorException(code: 0): Array to string conversion at C:\Users\Merlijn\AppData\Roaming\Composer\Laravel Projects\Forum\vendor\laravel\framework\src\Illuminate\Support\Str.php:354)
[stacktrace]
我试着用 JS
和 .toString()
将 JS
数组变成 1 个长字符串,但仍然出现同样的错误。
JS 数组的样子例如:
2: Array(2)
0: "{"type":"Feature","properties":{},"geometry":
{"type":"Point","coordinates":[4.90694,52.385973]}}"
1: {type: "Name", Status: "Insert pointer name"}
length: 2
__proto__: Array(0)
3: "{"type":"Feature","properties":{},"geometry":
{"type":"Point","coordinates":[4.90694,52.385973]}}"
length: 4
javascript代码ajax:
function saveInDb() {
if (typeof arrayAllDrawings !== 'undefined' && arrayAllDrawings.length > 0) {
$.ajax({
method: 'POST', said in the route
url: '{{ route('map.store') }}', gave in the route
data: {'mapData' : arrayAllDrawings},
success: function(response){
console.log(response);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(JSON.stringify(jqXHR));
console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
}
});
} else {
alert('The map has nothing on it!')
}
}
控制器:
public function store(Request $request)
{
// dd($request);
// $mapData = Input::get('mapData');
// dd($mapData);
if(request()->ajax()){
$mapData = Input::get('mapData');
$map = new Map;
$map->user_id = auth()->user()->id;
$map->map = $mapData;
$map->save();
return response()->json(['status' => 'succes', 'message' => 'saved in database','data' => $mapData]);
} else {
return response()->json(['status' => 'fail', 'message' => 'this is not json']);
}
}
您可以通过使用以下方法将数组转换为字符串来轻松管理它:
json_encode($your_array); or serialize($your_array);
反向可以完成:
json_decode($string); or unserialize($string);
关于以上功能的使用。
您可以将 javascript 数组转换为 json
JSON.stringify(arrayAllDrawings)
然后你可以得到它的形式是 json 并且把这个 json 转换成
json_encode(json_decode($mapData,true));
然后存入数据库
我正在尝试发送一个 array
到我的控制器以保存在数据库中,如果在这种情况下我制作 array a string
它将保存到数据库中。
所以当 data: {'mapData' : arrayAllDrawings},
它崩溃时(arrayAllDrawings 是一个数组)。但是当它像这样 data: {'mapData' : 'string'},
这样的字符串被硬编码时,它会通过 success message
当服务器抛出 500 内部服务器错误时来自 storage/logs
的错误日志
[2019-03-28 08:40:09] local.ERROR: Array to string conversion {"userId":1,"exception":"[object] (ErrorException(code: 0): Array to string conversion at C:\Users\Merlijn\AppData\Roaming\Composer\Laravel Projects\Forum\vendor\laravel\framework\src\Illuminate\Support\Str.php:354) [stacktrace]
我试着用 JS
和 .toString()
将 JS
数组变成 1 个长字符串,但仍然出现同样的错误。
JS 数组的样子例如:
2: Array(2)
0: "{"type":"Feature","properties":{},"geometry":
{"type":"Point","coordinates":[4.90694,52.385973]}}"
1: {type: "Name", Status: "Insert pointer name"}
length: 2
__proto__: Array(0)
3: "{"type":"Feature","properties":{},"geometry":
{"type":"Point","coordinates":[4.90694,52.385973]}}"
length: 4
javascript代码ajax:
function saveInDb() {
if (typeof arrayAllDrawings !== 'undefined' && arrayAllDrawings.length > 0) {
$.ajax({
method: 'POST', said in the route
url: '{{ route('map.store') }}', gave in the route
data: {'mapData' : arrayAllDrawings},
success: function(response){
console.log(response);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(JSON.stringify(jqXHR));
console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
}
});
} else {
alert('The map has nothing on it!')
}
}
控制器:
public function store(Request $request)
{
// dd($request);
// $mapData = Input::get('mapData');
// dd($mapData);
if(request()->ajax()){
$mapData = Input::get('mapData');
$map = new Map;
$map->user_id = auth()->user()->id;
$map->map = $mapData;
$map->save();
return response()->json(['status' => 'succes', 'message' => 'saved in database','data' => $mapData]);
} else {
return response()->json(['status' => 'fail', 'message' => 'this is not json']);
}
}
您可以通过使用以下方法将数组转换为字符串来轻松管理它:
json_encode($your_array); or serialize($your_array);
反向可以完成:
json_decode($string); or unserialize($string);
关于以上功能的使用。
您可以将 javascript 数组转换为 json
JSON.stringify(arrayAllDrawings)
然后你可以得到它的形式是 json 并且把这个 json 转换成
json_encode(json_decode($mapData,true));
然后存入数据库