Laravel执行GET而不是POST请求

时间:2017-07-17 11:08:28

标签: php ajax laravel post laravel-5.4

我有表格将数据POST到Controller并更新数据库

我的orders.blade.php

中的表单
<form method="post" enctype="multipart/form-data">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <select name="selectdb" required>
        <option value="" disabled selected>Select Delivery Boy</option>
        @foreach($delvery_boys as $delvery_boy)
          <option name="selectdb" data-oid="{{$order->id}}" value="{{$delvery_boy->id}}">{{$delvery_boy->name}}</option>
        @endforeach
    </select>
    <button type="submit" class="assigndb-btn btn-floating waves-effect waves-light">
        <i class="material-icons">send</i>
    </button>
</form>

我正在我的orders.blade.php

中向控制器执行表单数据的ajax POST请求
$(document).one("click", ".assigndb-btn", function () {

    $('form').submit(function(event) {
        event.preventDefault();

        var order_id = $(this).find(":selected").data('oid');
        var delivery_boy_id = $(this).find(":selected").val();
        var delivery_boy_name = $(this).find(":selected").text();

        $.ajax({
            url: '{{ url('/manager/assign_orders') }}',
            type: 'POST', 
            data: {"order_id":order_id,"delivery_boy_id":delivery_boy_id},
            success: function(data) {
                console.log(data);
            },
            error: function(error) {
                console.log(error);
            }
        });

    });

});

在我的OrdersController.php我有更新发布数据的逻辑

public function assignDeliveryBoy(Request $request)
{
    $assign_delivery_boy = Order::where('id', $request->order_id)->update(['delivery_boy_id' => $request->delivery_boy_id]);

    $data = [
        'success' => true,
        'message' => 'Order has been asigned'
    ];

    return response()->json($data);
}

我的路线是

Route::group(['prefix' => 'manager', 'middleware' => ['auth','roles'], 'roles' => 'manager'], function() {
    Route::post('/assign_orders', 'OrdersController@assignDeliveryBoy')->name('assignOrder');
});

当我提交表单时,它会调用assign_order路径并更新数据库

但是在我的控制台中我得到了页面的html代码,我提交表单基本上它正在执行GET而不是POST

当我在浏览器网络中检查时,响应是

Request URL:http://localhost:8000/manager/orders //but i am posting to http://localhost:8000/manager/assign_orders
Request Method:GET
Status Code:200 OK
Remote Address:127.0.0.1:8000
Referrer Policy:no-referrer-when-downgrade

我真的不明白出了什么问题

谢谢

3 个答案:

答案 0 :(得分:1)

试试这个:

        $.ajax({
        url: '{{ route('assignOrder') }}',
        type: 'POST', 
        data: {"order_id":order_id,"delivery_boy_id":delivery_boy_id},
        success: function(data) {
            console.log(data);
        },
        error: function(error) {
            console.log(error);
        }
    });

答案 1 :(得分:1)

getMessage()

答案 2 :(得分:1)

试试这段代码。

$.ajax({
    url: "{{ url('/manager/assign_orders') }}",
    type: 'POST', 
    data:{
         "order_id":order_id,
         "delivery_boy_id":delivery_boy_id,
         '_token':'{{ csrf_token() }}'
    },
    success: function(data) {
        console.log(data);
    },
    error: function(error) {
        console.log(error);
    }
});

让我知道它是否有效。