laravel 5.8在ajax请求中响应500内部服务器错误

时间:2019-03-28 08:51:20

标签: php jquery ajax laravel laravel-5.8

如果在这种情况下,我将array存入数据库,则尝试将array a string发送到我的控制器以保存在数据库中。 因此,当data: {'mapData' : arrayAllDrawings},崩溃时(arrayAllDrawings蜂鸣数组)。但是,当它像这样的data: {'mapData' : 'string'},这样的字符串被硬编码时,它将直接通过success message

并保存在数据库中

当服务器引发500个内部服务器错误时,来自storage/logs的错误日志

  

[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]

我尝试使用JSJS的{​​{1}}数组转换为1个长字符串,但仍然给出相同的错误。

例如,JS数组的外观:

.toString()

javascript代码ajax:

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

控制器:

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!')
        }
    }

2 个答案:

答案 0 :(得分:1)

您可以通过使用以下命令将数组转换为字符串来轻松进行管理:

json_encode($your_array); or serialize($your_array);

可以进行反向操作:

json_decode($string); or unserialize($string); 

关于上述功能的使用。

答案 1 :(得分:1)

您可以将javascript数组转换为json

JSON.stringify(arrayAllDrawings)

然后您可以使用json的形式获取它,并通过

将此json转换
json_encode(json_decode($mapData,true));

,然后存储到数据库中。

相关问题