vue全局启用 emulateJSON 选项

一、解释

如果Web服务器无法处理编码为application/json的请求,你可以启用emulateJSON选项。

启用该选项后,请求会以application/x-www-form-urlencoded作为Content-Type,就像普通的HTML表单一样

二、代码

1、全局启用代码

 // 全局启用 emulateJSON 选项
    Vue.http.options.emulateJSON = true;
  this.$http.post('api/addproduct', { name: this.name } ).then(result => {
            if (result.body.status === 0) {
              // 成功了!
              // 添加完成后,只需要手动,再调用一下 getAllList 就能刷新品牌列表了
              this.getAllList()
              // 清空 name 
              this.name = ''
            } else {
              // 失败了
              alert('添加失败!')
            }
          })

2、没有全局启用代码

    this.$http.post('api/addproduct', { name: this.name }, { emulateJSON: true }).then(result => {
            if (result.body.status === 0) {
              // 成功了!
              // 添加完成后,只需要手动,再调用一下 getAllList 就能刷新品牌列表了
              this.getAllList()
              // 清空 name 
              this.name = ''
            } else {
              // 失败了
              alert('添加失败!')
            }
          }) 
原文地址:https://www.cnblogs.com/wangyuxue/p/11842360.html