提问人:M kumar 提问时间:9/14/2023 最后编辑:brombeerM kumar 更新时间:9/14/2023 访问量:41
路由 admin/bill 不支持 POST 方法。支持的方法:GET、HEAD
The POST method is not supported for route admin/bill. Supported methods: GET, HEAD
问:
在Laravel中,我使用ajax通过刀片模板从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>
刀片.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>
我正在尝试在不加载页面的情况下提交数据,并使用一个控制器函数获取数据。
答:
0赞
Abinash Bhatta
9/14/2023
#1
在表单标记中,请按以下方式添加操作
<form method="post" action="javascript:void(0)" id="addProduct">
还有一件事
请将该行修改为 .因为我们没有从按钮中获取值。
如果它不起作用,请添加一个按钮。jQuery(this).serialize()
jQuery('#addProduct').serialize
type="submit"
0赞
Repox
9/14/2023
#2
你的jQuery已经很坏了,这导致表单被发布到你已经所在的页面;并且由于您当前的路由设置,您会收到路由不支持 POST 的错误。
您应该能够通过改用该函数并定位正确的表单元素来使 JavaScript 工作:on()
jQuery('#add').on('click', function(e) {
e.preventDefault();
var formData = jQuery('#addProduct').serialize();
$.ajax({
url: "{{ route('admin.bill.add') }}",
type: "post",
data: formData,
success: function(response){
console.log(response);
}
});
});
评论