路由 admin/bill 不支持 POST 方法。支持的方法:GET、HEAD

问题描述 投票:0回答:1

在 Laravel 中,我使用 ajax 通过 Blade 模板从 MySQL 数据库添加和获取数据,我在 onsubmit 事件上添加了表单求和。请帮助我。

我的代码是。 路线.php

Route::get('admin/bill',[BillingController::class, 'bill'])->name('billing');
    
Route::post('admin/billing/add',[BillingController::class, 'billadd'])->name('admin.bill.add');

控制器.php

public function billadd(Request $request){

        $request->validate([
            'customer_name'=>'required',
            'mobile_number'=>'required',
            'current_date'=>'required',
            'current_time'=>'required',
            'bill_number'=>'required',
            'product_name'=>'required',
            'batch_number'=>'required',
            'variant'=>'required',
            'price'=>'required',
            'quantity'=>'required',
            'tax_percent'=>'required',            
        ]);

        $posBill = new PosBilling();

        $posBill->customer_name=$request->customer_name;
        $posBill->customer_phone=$request->mobile_number;
        $posBill->billing_date=$request->current_date;
        $posBill->billing_time=$request->current_time;
        $posBill->bill_number=$request->bill_number;
        $posBill->product_name=$request->product_name;
        $posBill->batch_number=$request->batch_number;
        $posBill->variant = $request->variant;

        $unitPrice = $posBill->price = $request->price;  // One product price
        
        $totalQuantity = $posBill->quantity = $request->quantity;  // Total quantity of product
        
        $totalAmount = $posBill->quantity_value = $unitPrice * $totalQuantity;  // Total amount of product
        

        // Discount Calculation

        $discountPercent = $posBill->discount_percent = $request->discount_percent;  // Discount percent
        
        $discountPrice= $posBill->discount_amt = ($totalAmount) * ($discountPercent / 100);   // Discount amount
       
        $amountWithDiscount= $posBill->amount_with_discount = $totalAmount - $discountPrice;  // with Discount amount of products
        
        // Tax Calculation

        $taxPercent= $posBill->tax_percent = $request->tax_percent;  // Tax percent of product
        
        $TaxPrice = $posBill->tax_amount = ($amountWithDiscount) * ($taxPercent / 100); // Amount of products tax
        
        $CGST = $TaxPrice / 2;  // CGST amount of products
        
        $SGST = $TaxPrice / 2;  // SGST amount of products
        
        $amountWithTax = $posBill->net_amount = $TaxPrice + $amountWithDiscount;  // with Tax amount of products

        $i= 1;

        if($i == 1){            
            $posBill->save();
            $i++;

            // echo $i;
        }
        
        if($i>1){

            $billNumber = $request->bill_number;

            $billingProduct = PosBilling::where('bill_number', $billNumber)->get();

            
        }           
        return response()->json(['billingProduct'=>$billingProduct]);
        
    }

刀片.php

 <form  method="post"  id="addProduct">
    @csrf
<div class="row">
                            <div class="col-2">
                                <div class="form-group">
                                    <label for="">Product Name <b>*</b></label>
                                    <input type="text" class="form-control" name="product_name" id="product_name"
                                        placeholder="Product Name">
                                </div>
                            </div>
                            <div class="col-2">
                                <div class="form-group">
                                    <label for="">Batch Number <b>*</b></label>
                                    <input type="text" class="form-control" name="batch_number" id="batch_number">
                                </div>
                            </div>
                            <div class="col-1">
                                <div class="form-group">
                                    <label for="">Variant <b>*</b></label>
                                    <select class="form-control" name="variant" id="variant">
                                        <option value="" selected disabled>Variant</option>
                                        @foreach ($variant as $item)
                                            <option value="{{ $item->id }}">{{ $item->unit_name }}</option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>
                            <div class="col-1">
                                <div class="form-group">
                                    <label for="">Price <b>*</b></label>
                                    <input type="number" class="form-control" name="price" id="price">
                                </div>
                            </div>
                            <div class="col-1">
                                <div class="form-group">
                                    <label for="">Quantity <b>*</b></label>
                                    <input type="number" class="form-control" name="quantity" id="quantity">
                                </div>
                            </div>
                            <div class="col-2">
                                <div class="form-group">
                                    <label for="">Tax <b>*</b></label>
                                    <select class="form-control" name="tax_percent" id="tax_percent">
                                        <option value="" selected disabled>Tax</option>
                                        <option value="0">0% GST</option>
                                        <option value="5">5% GST</option>
                                        <option value="12">12% GST</option>
                                        <option value="18">18% GST</option>
                                        <option value="28">28% GST</option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-2">
                                <div class="form-group">
                                    <label for="">Discount[%]</label>
                                    <input type="discount" class="form-control" name="discount_percent" id="discount_percent"
                                        placeholder="Ex- 5, 10, 30, etc">
                                </div>
                            </div>
                            
                            <div class="col-1">
                                <div class="form-group">
                                    {{-- <button><a href="#" class="btn btn-success" id="add">Add</a></button> --}}
                                    <button class="btn btn-success" id="add" >Add</button>
                                </div>
                            </div>
                        </div>
                    </form>

blade.php(ajax 代码)

<script>
                        jQuery('#add').onsubmit(function(e){
                            e.preventDefault();
                            alert("Test");
                            break;
                            var formData = jQuery(this).serialize();
                            $.ajax({
                                url: "{{ route('admin.bill.add') }}",
                                type: "post",
                                data: formData,
                                success: function(response){                                    
                                    console.log(response);

 }
                            });
                        });
                    </script>

我正在尝试在不加载页面的情况下提交数据并使用一个控制器功能获取数据。

ajax laravel post get head
1个回答
0
投票

在表单标签中请按以下方式添加操作

<form  method="post" action="javascript:void(0)"  id="addProduct">

还有一件事 请将行

jQuery(this).serialize()
修改为
jQuery('#addProduct').serialize
。因为我们没有从按钮获取值。 如果它不起作用,请添加一个按钮
type="submit"

© www.soinside.com 2019 - 2024. All rights reserved.