我正在使用Laravel 5.
我有一个"命令"像这样的资源:
Route::resource('orders', 'OrdersController');
这会创建以下路线:
| | POST | orders | orders.store
| App\Http\Controllers\OrdersController@store | web |
| | GET|HEAD | orders | orders.index | App\Http\Controllers\OrdersController@index | web |
| | GET|HEAD | orders/create | orders.create | App\Http\Controllers\OrdersController@create | web |
| | DELETE | orders/{order} | orders.destroy | App\Http\Controllers\OrdersController@destroy | web |
| | GET|HEAD | orders/{order} | orders.show | App\Http\Controllers\OrdersController@show | web |
| | PUT|PATCH | orders/{order} | orders.update | App\Http\Controllers\OrdersController@update | web |
| | GET|HEAD | orders/{order}/edit | orders.edit | App\Http\Controllers\OrdersController@edit | web |
因此,我正在尝试使用jQuery POST到我的订单/商店,如下所示:
function StoreOrder(name, supplier, quantity, catnum, notes, urgency){
$.ajax({
type: 'POST',
dataType: 'text',
url: 'orders/store',
data: {
_token: CSRF_TOKEN,
name: name,
supplier: supplier,
quantity: quantity,
catnum: catnum,
notes: notes,
urgency: urgency
},
error: function(jqXHR, textStatus, errorThrown){
console.log(JSON.stringify(jqXHR));
console.log('AJAX Error: ' + textStatus + ": " + errorThrown);
},
success:function(data){
Materialize.toast(data);
//Clear the form
}
})
}
但我收到了一个' MethodNotAllowed'错误。
这是我第一次尝试使用带有laravel的AJAX。我在自己的程序代码中使用AJAX非常舒服,但是现在和Laravel一起,我遇到了一些小问题。我在这里缺少什么?
谢谢!
编辑:已实现我应该显示我的控制器代码:
<?php
namespace App\Http\Controllers;
use App\Announcement;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Redirect;
use View;
use Auth;
class OrdersController extends Controller {
public function index(){
//show my page
}
public function store(Order $order){
//get a pleasing time stamp
$today = date("F j, Y, g:i a");
//stores a post in the database
$order->create([
'name' => Request::input('name'),
'supplier' => Request::input('supplier'),
'quantity' => Request::input('quantity'),
'catnum' => Request::input('catnum'),
'notes' => Request::input('notes'),
'urgency' => Request::input('urgency'),
'author' => Auth::user()->username,
'date' => $today,
'status' => 'Processing'
]);
return ('Order created successfully.');
}
}