Javascript [Vue warn]:v-on处理程序中出现错误:“Vue warn”;SyntaxError:意外标记,JSON格式,位置76“;
当我试图将这个JSON数据添加到数据库中时,它给了我上面的错误Javascript [Vue warn]:v-on处理程序中出现错误:“Vue warn”;SyntaxError:意外标记,JSON格式,位置76“;,javascript,json,vue.js,axios,Javascript,Json,Vue.js,Axios,当我试图将这个JSON数据添加到数据库中时,它给了我上面的错误 var tax_setting = '{ "total_tax_settings": { "charge_tax": ' + this.taxInfoRadioButton + ', "tax_settings" : [' + '{ "tax_name": ' + this.taxName + ', "tax_ra
var tax_setting = '{ "total_tax_settings": { "charge_tax": ' + this.taxInfoRadioButton +
', "tax_settings" : [' +
'{ "tax_name": ' + this.taxName +
', "tax_rate": ' + this.taxRate +
', "tax_included_in_price": ' + this.taxIncludeInPrice + ' } ]}}';
此JSON的单选按钮:
<v-radio-group v-model="taxInfoRadioButton">
<v-radio color="primary" label="Do not charge tax on purchases" value="1"></v-radio>
<v-radio color="primary" label="Charge tax on purchase" value="2"></v-radio>
</v-radio-group>
data() {
return {
taxInfoRadioButton: '1'
}
}
到底是什么问题我搞不清楚。请帮帮我。使用JSON时要记住的数字1规则是
永远不要使用自己的JSON
创建JSON的最佳方法是首先创建要序列化的对象,然后将其传递给JSON.stringify()
。因为您使用的是Axios,所以您甚至不必这样做,因为Axios是为您而做的;您只需将要发布的对象传递给它
const tax\u设置={
总税设置:{
charge\u tax:这个按钮,
税务设置:[{
税务名称:this.taxName,
税率:这个税率,
含税价格:此项含税价格
}]
}
}
post('/api/data/user/save business settings',{tax_setting})
.然后(…)
为什么最初将税务设置创建为字符串?为什么不立即创建对象呢?JSON字符串中的字符串值必须用双引号括起来。例如,“{”tax_name:“'+this.taxName+”“+
@depperm,我必须收集数据并将它们包装到一个JSON
中,因为我必须将它们保存到数据库中。@Teemu,好的。但这不会造成问题!!我已经存储了其他类似的数据。但我认为你提到的是最好的方法。谢谢。错误发生在这里:var tax\u setting=JSON.parse(tax_设置)
,因为您创建的JSON字符串无效,字符串值必须用双引号括起来。原始的tax_设置
只是一个JS字符串。
<v-text-field name="taxName" label="Tax Name" id="taxName" v-model="taxName"></v-text-field>
data() {
return {
taxName: ''
}
}
<v-select hide-details v-bind:items="willTaxInclude" v-model="taxIncludeInPrice" label="Select" single-line menu-props="bottom" ></v-select>
data() {
return {
willTaxInclude: [
{ text: "Yes", value: 1 },
{ text: "No", value: 0 }
],
taxIncludeInPrice: '0',
}
}
axios.post('/api/data/user/save-business-settings', {
tax_setting: tax_setting,
}).then((res) => {
console.log(res);
}).catch((err) => {
console.log(err);
})