javascript - 如何像在 python 中一样发送请求并返回站点响应?
问题描述
为什么我在 js 中的代码不发送帖子数据而在 python 中有效?
在js中
const options = {
url: "https://asd.com",
method: 'POST',
headers: {
'Accept': 'application/json',
'Accept-Charset': 'utf-8',
'User-Agent': 'my-reddit-client',
'data': {"user.login":"login","user.senha":"pass"}
}
};
request(options, function(err, resx, body) {
console.log(resx);
res.send(body)
});
在蟒蛇
import requests
data = {
"user.login":"login",
"user.senha":"pass"
}
r = requests.post('https://asd.com', params=data)
print(r.text)
我只想发送一个没有 json 格式的自定义表单,但它永远不会起作用。
解决方案
POST
在 vanilla JS中提出请求:
fetch("https://asd.com", {
method: 'POST',
body: {"user.login":"login","user.senha":"pass"},
headers: {
'Accept': 'application/json',
'Accept-Charset': 'utf-8',
'User-Agent': 'my-reddit-client',
}
}).then(res => res.json())
我建议您也阅读有关 fetch API 的信息,这样您在 JS https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API中提出任何其他类型的请求就不再困难了/Using_Fetch。
推荐阅读
- c++ - 为什么这段代码抱怨“非类型模板参数不能有类型”?
- regex - 在 pyspark 的 regexp_replace 函数中使用字典
- javascript - JavaScript 函数和事件
- python-3.x - 用 python Beautifulsoup 抓取
- java - 错误:Model::findById 需要参数 id loopback android
- javascript - 编辑最小化 javascript 的源代码
- c# - 将 ulong 从 c++ 转移到 c#
- python - 为什么python不返回None?
- php - Wordpress 插件 - 需要添加 PHP 功能
- php - 参数无效的 Pdo 错误