我有一个表单,该表单已在Android Web视图中打开,我想将数据保存到数据库,但是当我进行ajax调用并尝试打印数据时,显示的是空白数组,下面是我的代码:
<div class="card-body card-padding">
<form id="submitdataform">
@csrf
@foreach($formfields as $key=>$value)
<input type="hidden" name="campaign_id" value="{{$key}}"/>
@foreach($value as $attrkey)
<input type="hidden" name="attribute_id[]" value="{{$attrkey->id}}"/>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label class="pure-material-textfield-outlined w-100">
{{--<input placeholder=" " type="text" required>--}}
<?php
if ($attrkey->attribute_type == 'alpha') {
echo "<input placeholder=' ' type='text' name='attribute_value[]' required>";
} elseif ($attrkey->attribute_type == 'date') {
echo "<input placeholder=' ' type='date' name='attribute_value[]' required>";
} elseif ($attrkey->attribute_type == 'numberic') {
echo "<input placeholder=' ' type='number' name='attribute_value[]' required>";
}
?>
<span>{{$attrkey->attribute_name}}</span>
</label>
</div>
</div>
</div>
@endforeach
@endforeach
<button type="button" id="submitbtn">Submit</button>
</form>
</div>
现在进行ajax调用
$("#submitbtn").click(function (event) {
event.preventDefault();
var data = $("#submitdataform").serialize();
$.ajax({
type: "post",
url: "savecampaigndata",
data: {data: data},
contentType: 'application/x-www-form-urlencoded',
dataType: 'json',
success: function (data) {
// Android.passParams('dashboard');
},
error: function (data) {
// Android.passParams(url);
}
});
});
这是我要获取所有数据的控制器方法,
public function SaveCampaignData(Request $request)
{
return response()->json($request->all());
$data = $request->all();
$attribute_id = [];
$attribute_value = [];
$campaign_id = $data['campaign_id'];
$user_id = Auth::user()->id;
foreach ($data as $key => $value) {
if ($key == "attribute_id") {
foreach ($value as $attrkey) {
$attribute_id[] = $attrkey;
}
} else {
if ($key == "attribute_value") {
foreach ($value as $attrvalue) {
$attribute_value[] = $attrvalue;
}
}
}
}
$mainArray = array_combine($attribute_id, $attribute_value);
$currentdate = Carbon::now();
foreach ($mainArray as $key => $value) {
DB::table('campaign_attribute_values')->insert(
[
'campaign_id' => $campaign_id,
'campaign_attribute_id' => $key,
'user_id' => $user_id,
'campaign_attribute_value' => $value,
'created_at' => $currentdate,
'updated_at' => $currentdate,
]
);
}
if ($this->CheckSmsLimit() > 0) {
$this->checkAutoReply($campaign_id, $user_id);
}
return redirect()->back()->with('success', 'Request Registered');
// return response()->json(['status' => true, 'message' => 'Record saved succesfully', 'code' => 200]);
}
这是邮递员路线网址:
https://subdomain.xyz.com/savecampaigndata
但是它在我的控制器中提供了一个空白数组作为输出。
答案 0 :(得分:0)
尝试在ajax调用的url属性中添加“ /”,并在ajax调用中添加标头属性。
答案 1 :(得分:0)
$("#submitbtn").click(function (event) {
event.preventDefault();
var data = $("#submitdataform").serialize();
$.ajax({
type: "post",
url: "{{ url('savecampaigndata) }}",
data: {data: data},
success: function (data) {
// Android.passParams('dashboard');
},
error: function (data) {
// Android.passParams(url);
}
});
});
1. if you don't want to send csrf token
then
app/Http/Middleware/VerifyCsrfToken.php
and add this route's url to the except array
protected $except = [
'savecampaigndata'
];
2. if want
then add in head
<meta name="csrf-token" content="{{ csrf_token() }}">
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
in script
答案 2 :(得分:0)
您似乎收到空白响应的原因是因为csrf
。我看到您已将其包含在表单中,但是,因为您使用的是serialize
,它无法按预期的方式工作。
有几种添加csrf的方法:
如果脚本位于刀片文件中,则只需将其添加到数据对象中即可:
data: {
data: data,
_token: "{{ csrf_token() }}",
},
或
根据documentation,您可以在页面的<head>
内添加以下内容:
<meta name="csrf-token" content="{{ csrf_token() }}">
,然后在加入jQuery之后包括以下内容:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});