Javascript XMLHttpRequest Post 方法
嘿,我有一个关于使用 XMLHttpRequest 发布的问题。理论上,如果我要将用户名和密码发布到 https 域(不幸的是,我尚未开始工作),responseText 会更改为下一个网站,还是应该填写文本字段?通常发生的情况是,您通过浏览器导航到此页面,输入用户名和密码,当单击提交按钮时,它会使用 POST 方法,在后台进行一些身份验证并返回不同的页面。我觉得也许responseText甚至应该保持完全相同(这就是现在发生的情况),但我不知道,因为我没有这种事情的经验。
this.requests[1].open("POST", "https://" + this.address, true);
var query = "target=%2Fcgi-bin%2FStatusConfig.cgi%3FPage%3Dindex&userfile=&username=user&password=pass&log+in=Log+in";
this.requests[1].setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
this.requests[1].setRequestHeader("Content-length", query.length);
this.requests[1].setRequestHeader("Keep-Alive", 115);
this.requests[1].setRequestHeader("Connection", "keep-alive");
this.requests[1].setRequestHeader("Host", this.address);
this.requests[1].send(query);
this.requests[1].onreadystatechange = onReadyStateChange1;
然后基本上 onReadyStateChange1 在准备好时显示responseText。
任何可以阐明帖子和响应文本应该发生什么的信息都将非常感激。与获取新的登录页面的任何建议一样。
为了进一步澄清,我想做的是登录然后返回新页面,因为登录页面仅显示登录信息/功能,而登录后的页面有很多相关信息。我并没有尝试检查凭据,而是尝试让它(脚本)登录,以便它可以访问下一页。当然,凭证必须有效。
谢谢大家。
Hey So I have a question about posting using an XMLHttpRequest. In theory, if I am to post a username and password to an https domain (which I have yet to get working, unfortunately) would the responseText then change to the next website, or should the text fields become filled in? What normally happens is you navigate to this page via browser, enter a username and password, and it uses a POST method when the submit button is clicked, doing some authentication under the hood and returning a different page. I feel like maybe the responseText should even stay exactly the same (which is what happens now), but I don't know as I have no experience with this kind of thing.
this.requests[1].open("POST", "https://" + this.address, true);
var query = "target=%2Fcgi-bin%2FStatusConfig.cgi%3FPage%3Dindex&userfile=&username=user&password=pass&log+in=Log+in";
this.requests[1].setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
this.requests[1].setRequestHeader("Content-length", query.length);
this.requests[1].setRequestHeader("Keep-Alive", 115);
this.requests[1].setRequestHeader("Connection", "keep-alive");
this.requests[1].setRequestHeader("Host", this.address);
this.requests[1].send(query);
this.requests[1].onreadystatechange = onReadyStateChange1;
Then basically onReadyStateChange1 displays the responseText when ready.
Any light that could be shed on what SHOULD be happening with the post and responseText would be very appreciated. As would any advice in getting the new, logged into page.
For further clarification, what I'm trying to do is log in and then return the new page, because the login page displays only log in information/functionality and the page after logging in has a lot of relevant information. I'm not trying to check the credentials as much as I'm trying to get it (the script) to log in so it can access the next page. Granted, the credentials will have to be valid for that.
Thanks all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确的话,您正在尝试使用 AJAX 检查凭据?如果是这样,请始终进行服务器端验证,并且仅将
{"success": true, "redirect": "/loggedin.html"}
返回给客户端。然后准备一个回调函数来转发response.redirect或类似的东西。If I understood correctly, you're trying to check credentials using AJAX? If so, always do server-side validation, and only return
{"success": true, "redirect": "/loggedin.html"}
back to the client. Then have a callback function ready that forwardsresponse.redirect
or something similar.好吧,我发现该网站应该发送包含相关信息的查询,但它不会填写文本字段或任何内容。响应文本更改。
Well I found that the website is supposed to send a query with the relevant information and it would not fill out the text fields or anything. ResponseText changes.