404 Not Found在向Laravel Controller发送后AJAX请求时出现错误

时间:2017-02-15 15:29:01

标签: php jquery ajax laravel-5

我正在尝试通过ajax post call将dta从视图发送到Laravel Controller。它给了我错误404没有找到我检查了很多东西,但我找不到错误。

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

这是来自视图的AJax:

$.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);
                }
            },
        });

这是我的路线入口

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

1 个答案:

答案 0 :(得分:0)

在您的路由器中,你应该喜欢 -

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

和ajax -

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