jQuery ajax 请求在 Laravel 应用程序中返回 405(不允许 POST)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25736679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery ajax request returning 405 ( POST not allowed) in laravel app
提问by hfingler
I can't seem to figure this out. Is this an apache configuration? I've seen some filters.php configurations to add POST but if this was the problem it would be somewhere in laravel docs, right?
我似乎无法弄清楚这一点。这是apache配置吗?我已经看到了一些用于添加 POST 的过滤器.php 配置,但如果这是问题,它会出现在 laravel 文档中的某个地方,对吗?
Routes:
路线:
Route::get('orders/add', 'OrderController@add');
Route::resource('orders', 'OrderController');
Controller (REST methods are empty):
控制器(REST 方法为空):
class OrderController extends \BaseController {
public function add()
{
if (Request::ajax())
return "ajax request ";
else
return "not ajax";
}
...
jQuery:
jQuery:
function add()
{
var tid = $('#sites input[type=radio]:checked').attr('id');
$.ajax({
type: "POST",
url: 'add',
data: { tid: tid }
}).done( function (msg){
alert(msg);
});
}
Button to send:
发送按钮:
<button onclick="add()" id="formSubmit"> Carrinho </button>
And the error firefox shows me on console when I click the button:
当我单击按钮时,Firefox 在控制台上显示错误:
POST http://localhost/orders/add [HTTP/1.0 405 Method Not Allowed 17ms]
Thank you all.
谢谢你们。
回答by Mysteryos
Route::get
expects a GET HTTP header. U'll need to use Route::post
.
Route::get
需要一个 GET HTTP 标头。你需要使用Route::post
.
Instead of
代替
Route::get('orders/add', 'OrderController@add');
Route::get('orders/add', 'OrderController@add');
you should use
你应该使用
Route::post('orders/add', 'OrderController@add');
Route::post('orders/add', 'OrderController@add');
回答by bgallagh3r
You really don't need a /add route if you are using a resource controller as it has a create method on it already.
如果您使用的是资源控制器,则您真的不需要 /add 路由,因为它已经有了 create 方法。
OrdersController extends BaseController {
public function index() {} // show ALL orders
public function create() {} // show the form to create an order aka "add"
public function store() {} // get input from post.
public function update($order_id) {} // update an order resource
public function destroy($order_id) {} // destroy an order resource
}
In your ajax change the url to url: {{URL::route('orders.store')}},
and that should fix it.
在您的 ajax 中,将 url 更改为url: {{URL::route('orders.store')}},
应该修复它。