我想通过node.js在第三部分网站中 POST 数据,但出现错误 403。 我在浏览器中有以下代码:
$.ajax("/login",{ /// here i am in https://255.255.255 /// I am obscuring the real host ip
data:{
username:"username",
password: "password",
autologin:"true"},
method:"POST"
}).done(function(msg) {
console.log( msg );
/// another $.ajax request here
});
工作正常。当我尝试在node.js中使用它时,问题就开始了,在SO用户的帮助下,我得到了以下代码:
var request = require('request');
request({
method: "POST",
baseUrl: "https://255.255.255",
uri: "/login",
form: {
username: "username",
password: "password",
autologin: "true"
}
},
function (error, httpResponse, body) {
if (error) {
console.error(error);
}
console.log(httpResponse.statusCode);
console.log(body);
});
在这里,我将httpResponse.statusCode设为403
-禁止使用。
我没想到是因为我的$.ajax
代码可以正常工作,并且不会向我返回此错误。
到目前为止,我已经尝试过:
我也想知道:
谢谢。
答案 0 :(得分:0)
jQuery代码实际上以纯文本形式发送用户名和密码,还是使用授权?发出请求时,您可以在浏览器控制台中签入。通常,用户/密码是作为“授权”标头发送的,您会在请求的标头中看到类似“授权:基本”的字样。
如果是这样,您还需要在Node中复制它。您可以查看the request documentation了解更多信息-这非常简单。