正如标题所说,我可以使用Vue资源发送非数组项目的发布请求,没有任何问题。
当发送数组数据时,响应是一个空数组。为什么会发生这种情况?
submit:function(){
var customizedExercises = this.customizedExercises;
this.$http.post('/api/customized-exercises', customizedExercises).then(function(response){
console.log(response);
}, function(response){
return response;
})
},
addSelectbox: function(){
this.customizedExercises.push({ weight:'',sets_duration:'',name:'',reps:'',exercise_day_id:'' })
}
},
数据
customizedExercises : [
{ weight:'',sets_duration:'',name:'',reps:'',exercise_day_id:'' }
],
HTML
<button type="submit" @click="addSelectbox">add select box</button>
<div v-for=" customizedExercise in customizedExercises" class="input-group">
<span class="input-group-addon">
<select class="selectpicker form-control" v-model="customizedExercise.name" name="name" id="name">
<option value="{{ exercise.name }}" v-for="exercise in exercises">{{ exercise.name }}</option>
</select>
</span>
<span class="input-group-addon">
<select class="selectpicker form-control" v-model="customizedExercise.sets_duration" name="sets_duration" id ="sets_duration">
<option v-for="number in numbers" value="{{number}}">{{number}}</option>
</select>
</span>
<span class="input-group-addon">
<select class="selectpicker form-control" v-model="customizedExercise.weight" name="weight" id="weight">
<option v-for="number in numbers" value="{{number}}">{{number}}</option>
</select>
{{数}}
<input type="hidden" name="exercise_day_id" v-model="customizedExercise.exercise_day_id" id="exercise_day_id" value="1" />
路线
Route::post('/api/customized-exercises',function() {
$inputs = Request::json()->all();
return $inputs;
});
答案 0 :(得分:1)
在将对象数组发送到PHP之前,您需要将对象数组传递给JSON。
customizedExercises = JSON.stringify(this.customizedExercises);