最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery ajax request returning 405 ( POST not allowed) in laravel app - Stack Overflow

programmeradmin1浏览0评论

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?

Routes:

Route::get('orders/add', 'OrderController@add');
Route::resource('orders', 'OrderController');

Controller (REST methods are empty):

class OrderController extends \BaseController {
   public function add()
    {
       if (Request::ajax())
           return "ajax request ";
       else 
           return "not ajax";
    }
...

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:

POST http://localhost/orders/add [HTTP/1.0 405 Method Not Allowed 17ms]

Thank you all.

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?

Routes:

Route::get('orders/add', 'OrderController@add');
Route::resource('orders', 'OrderController');

Controller (REST methods are empty):

class OrderController extends \BaseController {
   public function add()
    {
       if (Request::ajax())
           return "ajax request ";
       else 
           return "not ajax";
    }
...

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:

POST http://localhost/orders/add [HTTP/1.0 405 Method Not Allowed 17ms]

Thank you all.

Share Improve this question asked Sep 9, 2014 at 4:26 hfinglerhfingler 2,0144 gold badges30 silver badges38 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Route::get expects a GET HTTP header. U'll need to use Route::post.

Instead of

Route::get('orders/add', 'OrderController@add');

you should use

Route::post('orders/add', 'OrderController@add');

Source: Laravel routing documentation

You really don't need a /add route if you are using a resource controller as it has a create method on it already.

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.

发布评论

评论列表(0)

  1. 暂无评论