Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

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

 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>
like image 582
HashtagForgotName Avatar asked Mar 21 '19 11:03

HashtagForgotName


2 Answers

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.

like image 194
Nidhin Avatar answered Oct 18 '22 21:10

Nidhin


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

$.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);
    }
});
like image 43
Peter Avatar answered Oct 18 '22 22:10

Peter