该路由不支持GET方法。支持的方法:POST。laravel 5.8阿贾克斯

Has*_*ame 6 php ajax jquery laravel laravel-5.8

我试图更多地了解如何将ajax请求中的数据保存到laravel上的数据库中,在这种情况下,数据是原始(JSON FORMATTED)数据,只是为了查看其工作原理,如果我不将其添加到代码中(请注意保存到数据库中)

节省部分

 $input = Input::get('name');
 $json = new JsonTest;
 $json->json = $input;
 $json->save();
Run Code Online (Sandbox Code Playgroud)

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

   The GET method is not supported for this route. Supported methods: POST.
Run Code Online (Sandbox Code Playgroud)

因此,如何将文本区域保存到数据库中。数据库数据库

web.php

 Route::post('/customer/ajaxupdate', 'AjaxController@updateCustomerRecord')- 
 >name('jsonTest');
Run Code Online (Sandbox Code Playgroud)

控制器

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']);

    }

}
Run Code Online (Sandbox Code Playgroud)

刀片

 <!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>
Run Code Online (Sandbox Code Playgroud)

Nid*_*hin 5

我曾经遇到过同样的问题,问题出在从 http 到 https 的自动重定向。所以我在调用api时将url更改为https本身。


Pet*_*ter 3

您似乎没有name在 POST 数据中包含密钥。尝试这个:

$.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);
    }
});
Run Code Online (Sandbox Code Playgroud)