9

我试图更多地了解如何将 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.

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

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

    }

}

刀片

 <!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>
4

3 回答 3

7

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

于 2020-01-30T11:26:47.047 回答
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);
    }
});
于 2019-03-22T02:57:44.203 回答
0

如果您使用的是 insomnia 和 postman 等客户端应用程序,请不要忘记添加

Accept application/json

到标题。

于 2020-12-20T11:44:41.787 回答