【问题标题】:Vue Resource Post Request Content-TypeVue 资源发布请求内容类型
【发布时间】:2017-01-19 06:30:24
【问题描述】:

Vue-资源发布请求:

this.$http.post(form.action, new FormData(form)).then(function (response) {
    FetchResponse.fetch(this, response.data)
})

请求以 Content-Type:"application/json;charset=utf-8" 形式发送,但 PHP Post 无法显示任何数据。

设置标头 Vue-Resource:

request.headers.set('Content-Type', '');

但请求 Content-Type:", multipart/form-data;boundary=----WebKitFormBoundaryTsrUACAFB1wuhFOR"

查询的开头有一个逗号。

Jquery 发布请求:

$.ajax({
    url     : form.action,
    type    : 'POST',
    data    : new FormData(form),
    success : function (reqData) {
        FetchResponse.fetch(ss, reqData)
    },
});

相同的查询与 jQuery 无缝协作。 jQuery Content-Type: "multipart/form-data; boundary=----WebKitFormBoundaryTsrUACAFB1wuhFOR"

问题: https://github.com/vuejs/vue-resource/issues/398

【问题讨论】:

  • 为什么PHP不能显示发布的数据?你得到什么错误?刚才测试的时候无法设置内容类型,不过好像没关系——Laravel 接收数据就好了,甚至是文件上传。

标签: php laravel vue.js vue-resource vue-component


【解决方案1】:

请尝试发布一个简单的 JSON 对象并启用 'emulateJSON' vue-resource 选项:

const formData = {
    someProp: this.someProp,
    someValue: 'some value'
};

this.$http.post(this.postUrl, formData, {emulateJSON: true})
   .then(response => {
        console.log(response.body);
    }, response => {
       console.error(response.body);
    });

【讨论】:

  • 这是唯一对我有用的东西...为什么不将此设置为默认值?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-01
  • 1970-01-01
  • 2014-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多