我正在尝试向array
我的控制器发送一个以保存在数据库中,如果在这种情况下我将array a string
它保存到数据库中。所以当data: {'mapData' : arrayAllDrawings},
它崩溃时(arrayAllDrawings beeing一个数组)。但是当它像这样的字符串被硬编码时,data: {'mapData' : 'string'},
它会通过并保存在数据库中success message
storage/logs
服务器抛出 500 内部服务器错误时的错误日志
[2019-03-28 08:40:09] local.ERROR: 数组到字符串转换 {"userId":1,"exception":"[object] (ErrorException(code: 0): 数组到字符串转换在 C: \Users\Merlijn\AppData\Roaming\Composer\Laravel Projects\Forum\vendor\laravel\framework\src\Illuminate\Support\Str.php:354) [stacktrace]
我尝试JS
用 in 将数组变成 1 个长字符串,JS
但.toString()
仍然给出了同样的错误。
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']);
}
}