我使用$("form").serialize()
提交表单数据。虽然我从一个方法返回值它工作正常。我的方法代码如下。
public function store(Request $request)
{
$list = @$request['lists'];
$total_amount = @$request->total_amount;
$r_g_amount = @$request->r_g_amount;
$type = @$request->type;
$cash = @$request->cash;
$credit = @$request->credit;
$bank = @$request->bank;
$from = @$request->from;
$to = @$request->to;
return $cash;
}
它向我发送空值,如果我return $request->formdata
那么它会向我发送表格的所有细节。 formdata
是变量,我从ajax
传递为formdata:$("form").serialize()
。
那么如何将表单数据的值转换为variable
。
ajax请求
$.ajax({
url: "{{ route('HK.store') }}",
data: {
lists: list, total_amount: total_amount, formdata : $("form").serialize(), "_token": "{{ csrf_token() }}"
},
type: "POST",
success: function (data) {
console.log(data);
}
});
enter code here
答案 0 :(得分:1)
在Laravel的控制器功能中使用以下代码,
$box = $request->all();
$myValue= array();
parse_str($box['formdata'], $myValue);
print_r($myValue);
希望它会对你有所帮助!
答案 1 :(得分:0)
当您使用动态发布数据时,您必须确保存在变量。以下是如何获取所需变量的示例:
<meta name="viewport" content="width=device-width">
答案 2 :(得分:0)
您需要更新代码,如:
public function store(Request $request)
{
$list = $request->lists;
$total_amount = $request->total_amount;
$r_g_amount = $request->r_g_amount;
$type = $request->type;
$cash = $request->cash;
$credit = $request->credit;
$bank = $request->bank;
$from = $request->from;
$to = $request->to;
return response(['cash' => $cash]);
}
答案 3 :(得分:0)
您可以先将序列化的 formData 转换为 Object,然后将其发送到您的服务器:
const clientInfo= $('#checkoutForm').serialize();
const searchParams = new URLSearchParams(clientInfo);
clientInfo = Object.fromEntries(searchParams);// { 'type' => 'listing', 'page' => '2', 'rowCount' => '10' }
然后在 ajax 请求中,将 clientInfo
传递给 data
属性:
$.ajax({
url: ...,
method: "post",
data: clientInfo ,
success: function(){
}
})
在控制器中,当你添加有效载荷时,它看起来像这样:
array:6 [
"customer_name" => "Arely Torphy II"
"customer_email" => "lexi.kulas@jacobson.net"
"customer_phone" => "1-448-897-3923 x1937"
"address" => "1422 Ellie Stream Suite 859"
"post" => "37167"
"company_name" => "company"
]
现在,您可以轻松检索您想要的任何数据。