我正在尝试通过 API 在 localhost 上登录我的 Rocket.chat 应用程序。
当我向http://localhost:3000/api/login发送带有数据的 POST 时:{"user":"myusername","password":"mypassword"}
无论使用 xhr 请求、axios 还是 jquery ajax,我都会收到带有状态错误的响应 401。
但是当我使用 python virtualenv 或 curl 发送相同的数据时,响应为 200 并且状态成功。
我究竟做错了什么?为什么使用 javascript 发送时 POST 失败,使用 python 或 curl 发送时通过?
var xhr = new XMLHttpRequest();
xhr.open("POST", 'http://localhost:3000/api/login/', true);
xhr.send(JSON.stringify({
user: "myusername",
password: "mypassword"
}));
// result: {status: "error", message: "Unauthorized"}
我正在发送没有标头的登录请求,因为:
xhr.setRequestHeader('Content-Type', 'application/json');