vue.js - 我应该如何将代码从 XHR 转换为 Vue-Resource?
问题描述
我想将代码转换XHR
为Vue-Resource
请求。
XHR:
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
但出现错误:
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
body参数。
解决方案
您可以将data
作为second
参数传递给.post
方法。
它必须是 JSON 格式。
this.$http.post(
'url',
{ channel: 'default'},
{
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}
).then(response => {
console.log(response.body);
}, error => {
console.error(error);
});
推荐阅读
- java - 如何在没有相关实体的情况下在标准 api jpa 中进行 LEFT JOIN?
- java - 使用java将数据从cassandra迁移到多个文件
- .net - QueryContainerDescriptor Elasticsearch - 获取所有描述不等于 String.Empty 的文档
- node.js - nodejs crypto.createhash('sha256') 为同一个 S3 对象产生不同的哈希
- python - 使用 Python 从 URL 中隐藏 api 密钥
- ruby-on-rails - Rails 6 应用程序有两个基本控制器:一个继承自 ActionController::Base 另一个 ActionController::API
- python - 无法在python,pygame中添加图像
- c# - 将字符串值转换为 md5
- python-3.x - 如何在python中将此列表转换为可读格式
- java - Intelij 导出到 jar -> jar 不起作用(javafx 错误)