2017-02-15 107 views
0

我試圖通過ajax post調用將dta從視圖發送到Laravel Controller。它給了我錯誤404沒有發現我已經檢查了很多東西,但我找不到錯誤。404 Not Found發送向Laravel控制器發送AJAX請求後出現錯誤

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", "[email protected]"); 
+1

你的''savegame'在你的ajax中,'gameSave'在你的路線中。 –

+0

它的劑量效應 –

+0

不,我的意思是說,在你的ajax調用中,你有一部分URL作爲'savegame',但路由有'gamesave'。它沒有找到路線,因爲它們不一樣。 –

回答

0

在你的路由器,你應該有喜歡 -

// you should write exactly the same name defined in controller 
Route::post("/gameSave", "[email protected]"); 

和在阿賈克斯 -

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

請讓我知道...是否適用於你..如果有幫助,請接受作爲正確的答案和upvote –

+0

沒有它不適合我。 –