您想帮我吗?我有这样的一个ajax
var id = {{@$projectid}};
var datatab = JSON.stringify({data: change})
$.ajax({
url: 'save/'+id,
type: 'POST',
dataType: 'JSON',
data: {json:datatab},
success: function(response, textStatus, jqXHR) {
});
然后,我想将datatab作为json传递到json字段中的数据库 这是我在控制器中的保存功能
public function save($project_id=0, Request $request)
{
$project_id = $request->project_id;
$weight = $request->weight;
TaskPlanning::create([
'project_id' => $project_id,
'weight' => $weight
]);
return response()->json([]);
}
在数据库中,权重字段为json。我想将我的json文件从ajax保存为weight 这是我的模特
class TaskPlanning extends Model
{
protected $table = 'p_task_planning';
protected $fillable = ['project_id','weight'];
public function project()
{
return $this->belongsTo(Project::class,'project_id','id');
}
public function milestone()
{
return $this->belongsTo(Milestone::class,'milestone_id','id');
}
protected $casts = [
'weight' => 'array'
];
}
答案 0 :(得分:0)
尝试一下
var id = {{@$projectid}};
var datatab = JSON.stringify({data: 'change'});
$.ajax({
url: 'save/'+id,
type: 'POST',
dataType: 'JSON',
data: {_token : '{!! csrf_token() !!}',
weight : datatab,
project_id : id
},
success: function(response, textStatus, jqXHR) {
});
答案 1 :(得分:0)
使用 axios
代替$ .ajax。
请确保您添加了元标记
<meta name="csrf-token" content="{{ csrf_token() }}" >
然后
像这样使用axios ..它将自动加载csrf令牌
axios.post('save/'+id, data).then(function (response) {
//response
});