【问题标题】:404 Not Found Error comes up while sending post AJAX request to Laravel Controller404 Not Found Error 向 Laravel Controller 发送 post AJAX 请求时出现
【发布时间】:2017-02-15 15:29:00
【问题描述】:

我正在尝试通过 ajax post call 将视图中的 dta 发送到 Laravel 控制器。它给了我错误 404 not found 我检查了很多东西但我找不到错误。

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");

【问题讨论】:

  • 你的ajax中有savegame,你的路由中有gameSave
  • 它的影响
  • 不,我的意思是,在您的 ajax 调用中,您有一部分 URL 为 savegame,但路由有 gamesave。它没有找到路线,因为它们不一样。

标签: php jquery ajax laravel-5


【解决方案1】:

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

// 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') }}", 

【讨论】:

  • 请告诉我......它是否适合你......如果有帮助,请接受正确答案并投票
猜你喜欢
  • 2022-08-03
  • 2023-03-05
  • 2015-07-21
  • 2021-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
相关资源
最近更新 更多