shigg shigg - 5 months ago 8
Javascript Question

Ajax post req of array item gives me a null response

As the title says, I can send a post request for a non array item using Vue Resource with no problem.

When it comes to sending an array data the response is a null array.Why is this happening?

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:'' })
}
},


data

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>






{{number}}



<input type="hidden" name="exercise_day_id" v-model="customizedExercise.exercise_day_id" id="exercise_day_id" value="1" />




route

Route::post('/api/customized-exercises',function() {
$inputs = Request::json()->all();

return $inputs;


});

Answer

You need to pass the array of objects into a JSON before sending it over to PHP.

customizedExercises = JSON.stringify(this.customizedExercises);