laravel5.4中使用ajax请求控制器接口一直报错
admin.js
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$(".post-audit").click(function(){
var status = $(this).attr("post-action-status");
var post_id = $(this).attr("post-id");
var _self = $(this);
$.ajax({
url: "/admin/post/"+post_id+"/operate",
method: "POST",
data: {"status": status},
dataType:"json",
success: function handleSuccess(data){
console.log(data);
//操作后该项不显示
//_self.parent().parent().remove();
},
error: function(xhr, type){
console.log(type);
}
});
});
控制器
public function operate(Post $post)
{
return ['code'=>200,'msg'=>'测试'];
/*$this->validate(request(), [
"status" => 'required|in:-1,1',
]);
$post->status = request('status');
$post->save();
return response()->json([
'error' => 0,
'msg' => ''
]);*/
}
上面的请求浏览器控制台一直提示parsererror:
但是将返回数据写在路由里又是正常的
Route::post('/admin/post/{post}/operate',function(){
return ['code'=>200,'msg'=>'测试'];
})->where('post','[0-9]+');
浏览器控制台输出:
小弟新手,请大神多多指教