【问题标题】:Ajax post req of array item gives me a null response数组项的 Ajax post req 给了我一个空响应
【发布时间】:2016-06-11 02:10:19
【问题描述】:

正如标题所说,我可以毫无问题地使用 Vue Resource 发送对非数组项的发布请求。

在发送数组数据时,响应为空数组。为什么会发生这种情况?

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;


});

【问题讨论】:

  • 你把对象数组转换成JSON了吗
  • customizedExercises = JSON.stringify(this.customizedExercises);
  • 谢谢你我完全不知道:(
  • 现在能用吗,能用,请选择我的答案,如果其他人有同样的问题,他们会看到解决方案。

标签: javascript php laravel vue.js


【解决方案1】:

您需要先将对象数组传递到 JSON 中,然后再将其发送到 PHP。

customizedExercises = JSON.stringify(this.customizedExercises);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-19
    • 2013-05-03
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    • 2022-06-15
    相关资源
    最近更新 更多