簡體   English   中英

通過 Ajax 將數據從 Laravel 7 視圖傳遞到 controller

[英]Passing data from Laravel 7 view to controller via Ajax

我只需要使用 Ajax(在 View home.blade.php 中)將一個值從視圖傳遞給 controller。 所有解決方案都適用於 Laravel 5,它沒有幫助。

Ajax 來自 home.blade.php:

$datee="hello";

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

 $.ajax({
    type        : 'POST',
    url         : "insert2",
    contentType: "text",
    data:      datee ,
    dataType: 'json'
  });

路線:

Route::post('/insert2/{datee}', 'EditContdroller@show1');
Route::get('/home', 'HomeController@index')->name('home');

而EditController的方法:

  public function show1(Request $request){

                         $data= $request->datee;
//some work with $data...
                         return $json_Value;

    }

我收到錯誤 404 Post.../insert2 not found。請問您有什么想法嗎?

Route::post('/insert2/{datee}', 'EditContdroller@show1');

url:"insert2",

您的發布路線需要額外的參數,但您要求不帶參數

應該是 url: "insert2/something"

現在在 Controller 中,您傳遞的“某物”將變為變量 {datee}

如果要使參數可選,則應在 {datee} 中添加問號,使其成為 {datee?} 然后您的 AJAX 請求應該可以工作:(我在日期中添加了“?”問號)

Route::post('/insert2/{datee?}', 'EditContdroller@show1');

你正在通過:

data: datee,

它不像這樣工作。

要通過日期,我建議這樣做:

刪除 {datee} 部分

Route::post('/insert2', 'EditContdroller@show1');

在您的 AJAX 請求中修改您的數據,如下所示:

data: { datee: datee },

在您的 Controller 訪問 datee 值,如下所示:

$datee = $request->input('datee');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM