我的更新功能出现错误419。某些功能无法正常运行;这是我在index.blade.php中的ajax函数
$('#action1').click(function (){
var Ids = new Array();
var Names = new Array();
var Status = new Array();
if($(".selected").length){
$(".selected").each(function(){
Ids.push($(this).children(".id").html());
Names.push($(this).children(".company_name").html());
Status.push($(this).children(".status").html());
});
$.ajax({
url: 'update',
method:'POST',
dataType: 'json',
"id":Ids,
"company_name":Names,
"status":Status,
success: function(data){
$('#form1').html(data.success);
}
});
// alert(Ids);
// alert(Names);
// alert(Status);
}else{
alert("No row is selected.");
}
});
这是我的控制器文件:
public function update(Request $request)
{
$ids = $request->input('Ids');
$success_output = '';
$companies = companies::find($ids);
companies::whereIn('id',$ids);
$this->validate($request,
['company_name' => 'required',
'status' => 'required',
]);
$companies->company_name = $request->Names;
$companies->status = $request->Status;
$companies->save();
$success_output = '<div class="alert alert-success">Data Inserted</div>';
$output = array(
'errror' => $error_array,
'success' => $success_output
);
echo json_encode($output);
return redirect('/companies')->with('success','updated');
}
这是我的路线功能:
Route::post('update','AjaxController@update');
我想一次更新多行,但是更新功能可能不起作用。有人可以提出任何解决方案吗?
答案 0 :(得分:0)
您应该发送csrf_token
将此代码csrf令牌设置为所有ajax请求
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': 'your csrf token'
}
});
答案 1 :(得分:0)
将您的控制器更改为此
public function update(Request $request)
{
$ids = $request->input('id');
$success_output = '';
$companies = companies::find($ids);
companies::whereIn('id',$ids);
$this->validate($request,
['company_name' => 'required',
'status' => 'required',
]);
$companies->company_name = $request->input('company_name');
$companies->status = $request->input('status');
$companies->save();
$success_output = '<div class="alert alert-success">Data Inserted</div>';
$output = array(
'errror' => $error_array,
'success' => $success_output
);
echo json_encode($output);
return redirect('/companies')->with('success','updated');
}
和您的ajax
$('#action1').click(function (){
var Ids = new Array();
var Names = new Array();
var Status = new Array();
if($(".selected").length){
$(".selected").each(function(){
Ids.push($(this).children(".id").html());
Names.push($(this).children(".company_name").html());
Status.push($(this).children(".status").html());
});
$.ajax({
url: 'update',
method:'POST',
dataType: 'json',
data:{
"id":Ids,
"company_name":Names,
"status":Status,
},
success: function(data){
$('#form1').html(data.success);
}
});
// alert(Ids);
// alert(Names);
// alert(Status);
}else{
alert("No row is selected.");
}
});