doupingmao1903 2017-07-17 11:08
浏览 42
已采纳

Laravel执行GET而不是POST请求

I have form to POST data to Controller and update database

The form in my 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>

I am doing an ajax POST request of form data to controller in my orders.blade.php

$(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);
            }
        });

    });

});

And in my OrdersController.php i have the logic to updated the posted data

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);
}

My Route is

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

When i submit the form it suppose to hit the assign_order route and update the database

But in my console i am getting the html code of the page from where i submit the form basically it is executing GET instead of POST

As i checked in browser network whent the response is

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

i really don't understand what is wrong

thank you

  • 写回答

3条回答 默认 最新

  • douxunwei7083 2017-07-17 11:37
    关注

    Try this code.

    $.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);
        }
    });
    

    Let me know if it works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 cv::resize不同线程时间不同
  • ¥15 web课程,怎么做啊😭没好好听课 根本不知道怎么下手
  • ¥15 做一个关于单片机的比较难的代码,然后搞一个PPT进行解释
  • ¥15 python提取.csv文件中的链接会经常出现爬取失败
  • ¥15 数据结构中的数组地址问题
  • ¥15 maya的mel里,怎样先选择模型A,然后利用mel脚本自动选择有相同名字的模型B呢。
  • ¥15 Python题,根本不会啊
  • ¥15 会会信号与系统和python的来
  • ¥15 关于#python#的问题
  • ¥20 oracle RAC 怎么配置啊,配置