首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将代码从XHR转换为Vue-Resource?

如何将代码从XHR转换为Vue-Resource?
EN

Stack Overflow用户
提问于 2018-08-07 09:39:59
回答 1查看 279关注 0票数 0

我想将XHR中的代码转换为Vue-Resource请求。

XHR:

代码语言:javascript
复制
var data = "channel=default";

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
  if (this.readyState === 4) {
    console.log(this.responseText);
  }
});

xhr.open("POST", "url");
xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");

xhr.send(data);

下面是我在Vue-Resource中的代码,但是我得到了一个错误:

代码语言:javascript
复制
this.$http.post(
        'url',
        {
          headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
          body: 'channel=default'
        }
      ).then(response => {
          console.log(response.body);
      }, error => {
          console.error(error);
      });

我不知道我的vue代码有什么问题。我需要在身体参数中传递channel?default

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-07 09:44:58

可以将data作为second参数传递给.post方法。

它必须是JSON格式的.

代码语言:javascript
复制
this.$http.post(
    'url',
    { channel: 'default'},
    {
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    }
).then(response => {
    console.log(response.body);
}, error => {
    console.error(error);
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51723593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档