404 Not Found发送向Laravel控制器发送AJAX请求后出现错误
问题描述:
我试图通过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]");
答
在你的路由器,你应该有喜欢 -
// 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
没有它不适合我。 –
你的''savegame'在你的ajax中,'gameSave'在你的路线中。 –
它的剂量效应 –
不,我的意思是说,在你的ajax调用中,你有一部分URL作为'savegame',但路由有'gamesave'。它没有找到路线,因为它们不一样。 –