【问题标题】:Laravel Explode Ajax Request Containing Name and NumbersLaravel 爆炸包含名称和数字的 Ajax 请求
【发布时间】:2021-09-28 22:51:11
【问题描述】:

我正在使用laravel 8.4

我的路线:

Route::post('test',[\App\Http\Controllers\DataController::class, 'store'])->name('pos-test');

我的控制器:

public function store(Request $request)
{
 //   DB::table('data')->insert('product_code',$request->id);
    $badge = explode(' ', $request);
    $employee_id = $badge[0];

    \DB::table('data')->insert(['product_code'=> $employee_id]);       

    return response()->json(['success'=>'Product saved successfully.']);

}

Ajax 代码:

function handleBarcode(scanned_barcode) {
    //handle your code here....
    console.log(scanned_barcode);

            let _token   = $('meta[name="csrf-token"]').attr('content');
      event.preventDefault();
     $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
     });
  $.ajax({
        url: "{{ route('pos-test') }}",
        type: "POST",            // Can change this to get if required
        data: {
          code : scanned_barcode,
          _token: _token
        },
        success: function(data) {
                 $("#status").html(data);      
        },
        error: function(jqXHR, textStatus, errorThrown) {
               $("#status").text(textStatus);
               console.log(jqXHR);
        }
  });

};

请求是这样的 "555444 Razif Raziq" ,我想把它分解,所以我可以只在表中插入 "555444" 但在表列中 product_code 是 'POST'。

问题是如何解决它?谢谢

【问题讨论】:

  • 添加 ajax 代码并说明你有什么错误
  • 我只是添加了一个ajax代码和图片。
  • 什么是console.log(scanned_barcode);输出?

标签: laravel laravel-query-builder


【解决方案1】:

您必须在请求对象中分解正确的数据,而不是请求对象本身。

$badge = explode(' ', $request->code);

【讨论】:

    【解决方案2】:

    如果'code'值发送正确,就使用这个

    $request->代码

    public function store(Request $request)
    {
    \DB::table('data')->insert(['product_code'=> $request->code]);       
    
    return response()->json(['success'=>'Product saved successfully.']);
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-08
      相关资源
      最近更新 更多