我想使用 ajax 和 Laravel 来提交表单。但 Ajax post 方法现在允许 405 方法。我正在使用 Restful 路线。
我的代码如下:
我的路线文件如下:
Route::resource("a","AController");
我的js文件如下:
var host = "http://example.com/";
var url = host + "a/store";
$('#form_id').on('submit',function(e){
e.preventDefault();
var data = $(this).serialize();
$.ajax({
url:url,
data: data,
type: 'post',
dataType: 'json',
timeout: 1000,
error:function() { alert("Error Submitting Information") }
}).done(function(resp){
});
});
我的 Controller 文件:
public function store(Request $request){
$input = $request->all();
}
但它给出了 405 method not allowed 错误
最佳答案
删除/store
部分:
var host = "http://example.com/";
var url = host + "a/"; //<----
$('#form_id').on('submit',function(e){
e.preventDefault();
var data = $(this).serialize();
$.ajax({
url:url,
data: data,
type: 'post',
dataType: 'json',
timeout: 1000,
error:function() { alert("Error Submitting Information") }
}).done(function(resp){
});
});
默认情况下,laravel 会将任何 POST 请求定向到 AController
中的 store
函数
查看 Laravel 文档中的路由表:https://laravel.com/docs/5.3/controllers#resource-controllers
关于javascript - Laravel Restful路线ajax方法post 405方法不允许,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41810373/