jquery Laravel错误:路由addproduct不支持GET方法,支持的方法:POST

q5lcpyga  于 2024-01-07  发布在  jQuery
关注(0)|答案(3)|浏览(193)

我是一个初学者在laravel.我有问题尝试张贴到路由.我不使用任何形式(形式行动="”方法=“POST”).我认为问题可能是在我的控制器.
这是我的按钮:

  1. <div class="button0">
  2. <button
  3. type="button"
  4. class="btn btn-primary"
  5. style="margin:20px 150px 0 0; float: right; width:100px"
  6. id="btnInsert"
  7. >
  8. Insert
  9. </button>
  10. </div>

字符串
以下是我的剧本:

  1. $("#btnInsert").click(function () {
  2. var productid = $("#productid").val();
  3. var productname = $("#productname").val();
  4. var productprice = $("#productprice").val();
  5. var productdes = $("#productdes").val();
  6. var productimage = $("#productimage").val();
  7. var productcate = $("#cate").val();
  8. if (productid == "") {
  9. alert("Empty Product ID");
  10. } else {
  11. // it doesn't work after this
  12. $.post("/addproduct", {
  13. productid: productid,
  14. productname: productname,
  15. productprice: productprice,
  16. productdes: productdes,
  17. productimage: productimage,
  18. productcate: productcate,
  19. }),
  20. function (result) {
  21. if (result == 1) {
  22. alert("Record has been added");
  23. window.location.href = "/product";
  24. }
  25. };
  26. }
  27. });


这是我的路线:

  1. Route::post('/addproduct',[ProductController::class, 'AddProduct']);


这是我的控制器,这里可能有些错误。

  1. public function AddProduct(Request $request){
  2. try{
  3. $productid = $request->productid ;
  4. $productname = $request->productname;
  5. $productprice = $request->productprice;
  6. $productdes = $request->productdes;
  7. $productimage = $request->productimage;
  8. $categoryid = $request->categoryid;
  9. $result = DB::table('tblproduct')->insert(['ProductID'=>$productid,
  10. 'ProductName'=>$productname,
  11. 'ProductPrice'=>$productprice,
  12. 'ProductDes'=>$productdes,
  13. 'ProductImage'=>$productimage,
  14. 'CategoryID'=>$categoryid]);
  15. if ($result) {
  16. return redirect()->route('product.InsertProduct');
  17. } else {
  18. echo "Insert failed: " . DB::getErrorMessage();
  19. }
  20. }
  21. catch (Exception $ex){
  22. echo ($ex);
  23. }
  24. }

mgdq6dx1

mgdq6dx11#

首先点击这些工匠命令:

  1. php artisan route:clear
  2. php artisan route:cache

字符串
或者您可以使用

  1. php artisan optimize:clear
  2. php artisan optimize


然后在api.php文件中修改你的url:coz apis,

  1. $.post("api/addproduct", {


希望,它会帮助你

展开查看全部
qzlgjiam

qzlgjiam2#

你的方法也很好,但正如你所说,你是初学者,所以我建议你必须阅读laravel文档并练习MVC方法。

始终使用路由,并确保每条路由都有不同或唯一的名称,或者您可以创建一组路由
路线

  1. Add Controller in web.php | api.php on top.
  2. use App\Http\Controllers\ProductController;
  3. Route::post('/addproduct', [ProductController::class, 'AddProduct'])->name('add.product');

字符串

团体路线

  1. Route::controller(ProductController::class)->group(function () {
  2. Route::post('/addproduct','AddProduct')->name('add.product');
  3. Route::get('/listproduct','ListProduct')->name('list.product');
  4. });

  1. <form action="{{ route('add.product') }}" method="post" enctype="multipart/form-data"
  2. autocomplete="off">
  3. @csrf
  4. <div class="row">
  5. <div class="col-sm-12">
  6. <div class="row" style="margin-top: 15px;">
  7. <div class="col-sm-12">
  8. <div class="row">
  9. <div class="col-sm-6">
  10. <label for="horizontal-main-course-input"
  11. class="col-sm-5 col-form-label">Name</label>
  12. <div class="col-sm-12">
  13. <input type="text" class="form-control" name="name"
  14. id="name" placeholder="Enter Name Here">
  15. @if ($errors->has('name'))
  16. <span
  17. class="text-danger">{{ $errors->first('name') }}</span>
  18. @endif
  19. </div>
  20. </div>
  21. <div class="col-sm-6">
  22. <label for="horizontal-main-course-input"
  23. class="col-sm-5 col-form-label">Email</label>
  24. <div class="col-sm-12">
  25. <input type="text" class="form-control" name="price"
  26. id="price"
  27. placeholder="Enter Price Here">
  28. @if ($errors->has('price'))
  29. <span
  30. class="text-danger">{{ $errors->first('price') }}</span>
  31. @endif
  32. </div>
  33. </div>
  34. </div>
  35. </div>
  36. </div>
  37. </div>
  38. </div>
  39. <br />
  40. <div class="row">
  41. <div class="col-sm-12">
  42. <div class="col-sm-12" style="text-align: center;">
  43. <button type="submit" class="btn btn-primary w-md">SAVE</button>
  44. </div>
  45. </div>
  46. </div>
  47. </form>

创建控制器:

  1. namespace App\Http\Controllers;
  2. use Illuminate\Http\Request;
  3. use App\Models\Product;
  4. class ProductController extends Controller
  5. {
  6. public function index()
  7. {
  8. // Logic to display a list of products
  9. }
  10. public function create()
  11. {
  12. // Return a view for creating a new product
  13. return view('products.create');
  14. }
  15. public function store(Request $request)
  16. {
  17. // Validate and store the product
  18. $product = new Product();
  19. // Assign values from $request to $product properties
  20. $product->productid = $request->input('productid');
  21. $product->productname = $request->input('productname');
  22. // ... other properties
  23. $product->save();
  24. // Redirect to the products index page or do something else
  25. return redirect()->route('products.index');
  26. }
  27. }


创建模型Product和迁移连接或保存表中的数据。我希望这将帮助你,或者你可以问任何你想要的。快乐编码!

展开查看全部
g0czyy6m

g0czyy6m3#

你的Laravel代码很好,但你的JS代码有问题。
你忘记在post请求后使用done回调函数。

  1. $.post("/addproduct", {
  2. productid: productid,
  3. productname: productname,
  4. productprice: productprice,
  5. productdes: productdes,
  6. productimage: productimage,
  7. productcate: productcate,
  8. }).done(function (result) {
  9. if (result == 1) {
  10. alert("Record has been added");
  11. window.location.href = "/product";
  12. }
  13. });

字符串
希望它能帮助你

展开查看全部

相关问题