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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/55279293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:18:18  来源:igfitidea点击:

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

phpjqueryajaxlaravellaravel-5.8

提问by HashtagForgotName

I am trying to understand more about how to save data from ajax request into the database on laravel, The data in this case is raw (JSON FORMATTED) data just to see how it works it works fine if I dont add this to the code (note the saving into the data base)

我试图更多地了解如何将 ajax 请求中的数据保存到 Laravel 上的数据库中,这种情况下的数据是原始(JSON 格式)数据,只是为了看看它是如何工作的,如果我不将其添加到代码中,它可以正常工作(注意保存到数据库中)

the saving part

储蓄部分

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

it works fine but when I have this part from above in the code (the saving part) its gives an error

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

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

so how can I save the text area into the data base. the data base database

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

web.php

网页.php

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

The Controller

控制器

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

    }

}

The 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>

采纳答案by Peter

It looks like you are not including the namekey in your POST data. Try this:

看起来您没有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);
    }
});

回答by Nidhin

I faced the same problem once, Where the issue resides auto redirection from http to https. So the url I changed to https itself while calling api.

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