【发布时间】:2017-12-21 00:27:41
【问题描述】:
我有 weights 和 values 的 2 个输入框,然后我可以单击一个按钮自动创建更多。这部分工作正常。
现在的问题是服务器需要一个包含整数数组而不是字符串的 JSON formdata 请求。
postdata 应该是这样的:
{"stuff": {"weights": [2, 5, 3], "values": [654, 23, 3]}}
这就是它最终的样子:
{"stuff": {"weights": ["2", "5", "3"], "values": ["654", "23", "3"]}}
我已经尝试搜索如何将数据数组类型设置为int,如何将v-model保存为int等。
我相信这是所有相关代码:
<template>
..
<div v-for="(weight, index) in weights">
<div>
<label for="w">weight:</label>
<input v-model="weights[index]">
<label for="v">value:</label>
<input v-model="values[index]">
</div>
</div>
..
</template>
<script>
export default {
data () {
return {
weights: [],
values: []
}
},
methods: {
solve: function () {
var formData = {
stuff: {
weights: this.weights,
values: this.values
}
}
axios.post('http://localhost:8000/my/api/url', formData).then(response => {
console.log(response)
}).catch(e => {
console.log(e)
})
},
...
</script>
【问题讨论】: