此路由不支持 GET 方法。支持的方法:POST。 laravel 5.8 Ajax

The GET method is not supported for this route. Supported methods: POST. laravel 5.8 Ajax

我想了解更多有关如何将来自 ajax 请求的数据保存到 laravel 上的数据库的信息,本例中的数据是原始数据(JSON 已格式化)看看它是如何工作的 如果我不将它添加到代码中它工作正常(注意保存到数据库中)

储蓄部分

 $input = Input::get('name');
 $json = new JsonTest;
 $json->json = $input;
 $json->save();

它工作正常但是当我从上面的代码中得到这部分时(保存部分) 它给出了一个错误

   The GET method is not supported for this route. Supported methods: POST.

那么如何将文本区域保存到数据库中。数据库 database

web.php

 Route::post('/customer/ajaxupdate', 'AjaxController@updateCustomerRecord')- 
 >name('jsonTest');

控制器

public function updateCustomerRecord(Request $request)
{

    if(request()->ajax()){

        $input = Input::get('name');
        //$input = $request->all();
        $json = new JsonTest;
        $json->json = $input;
        $json->save();

        return response()->json(['status' => 'succes', 'message' => 'saved in database']);

    } else {

        return response()->json(['status' => 'fail', 'message' => 'this is not json']);

    }

}

blade

 <!DOCTYPE html>
 <html lang="en">
<head>
<title>JavaScript - read JSON from URL</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<meta name="csrf-token" content="{{ csrf_token() }}" />
</head>

<body>
<textarea  oninput="myFunction()" id="input" name="input" style="height: 
500px;width: 500px">
</textarea>

<script>
const warning = 'This json is not correctly formatted';
const text = {status: "failed", message: "this is not correct json format"};

$.ajaxSetup({
    headers: {

        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')

    }
});

function myFunction(){

    let input = document.getElementById("input").value;

    try {
        let id = JSON.parse(input);
        if (id && typeof id === "object") {
            $.ajax({
                method: 'POST', // Type of response and matches what we said in the route
                url: '{{ route('jsonTest') }}', // This is the url we gave in the route
                data: {'id' : id}, // a JSON object to send back
                success: function(response){ // What to do if we succeed
                    console.log(response);
                },
                error: function(jqXHR, textStatus, errorThrown) { // What to do if we fail
                    console.log(JSON.stringify(jqXHR));
                    console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
                }
            });
        }
    }
    catch (e) {
        console.log(warning);
        console.log(text);
    }
    return false;
}
</script>

</body>
</html>

您的 POST 数据中似乎没有包含 name 键。试试这个:

$.ajax({
    method: 'POST', // Type of response and matches what we said in the route
    url: '{{ route('jsonTest') }}', // This is the url we gave in the route
    data: {'name' : 'testing'}, // <-- this is your POST data
    success: function(response){ // What to do if we succeed
        console.log(response);
    },
    error: function(jqXHR, textStatus, errorThrown) { // What to do if we fail
        console.log(JSON.stringify(jqXHR));
        console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
    }
});

我曾经遇到过同样的问题,问题在于从 http 到 https 的自动重定向。 所以 url 我在调用 api.

时更改为 https 本身

如果你使用的是insomnia和postman之类的客户端,别忘了添加

Accept application/json

到页眉。