简体   繁体   中英

404 Not Found Error comes up while sending post AJAX request to Laravel Controller

I am trying to send dta from view to Laravel Controller via ajax post call. and it giving me error 404 not found i have checked things many but i cant find mistake.

class AttemptController extends Controller {
     public function postSavegame(Request $request) {
        $data = $request->all();
        print_r($data);
        //insert data to db
        $id = $this->model->insertRow($data, $request->input($data));

        // Insert logs into database
        if ($id != '') {
             \SiteHelpers::auditTrail($request, 'New Data with ID ' . $id . ' Has been Inserted !');
            echo('Done');
        } 
    }
}

here is the AJax from view:

$.ajax({
            method: 'POST',
            url: "{{ URL::to('Attempt/savegame') }}",
            data: {
                quiz_id: localStorage.quiz_id,
                user_id: "{{Session::get('uid')}}",
                total_score: localStorage.achivePoints, // 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);
                }
            },
        });

and this is my route entry

Route::post("gamesave", "AttemptController@gamesave");

In your router you should have like-

// you should write exactly the same name defined in controller
Route::post("/gameSave", "AttemptController@postSavegame");

and in ajax -

// you have to use exaclty same url specified in router  
url :   "{{ URL::to('/gameSave') }}", 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM