xhttp将用户名和密码发送到url

xhttp send username and password to url

本文关键字:url 密码 用户 xhttp      更新时间:2023-09-26

我需要从站点example.com/something.aspx?id=1获取信息,但只有当用户登录时才能访问此页面。

因此,我使用以下脚本登录用户,然后从页面中获取信息,但我想知道脚本的格式是否正确。

<script>
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "http://example.com/login", {username: 'bob', password: '123'}, true);
xhttp.send();
$.getJSON('http://www.example.com/something.aspx?id=1', function(data) {
console.log(data);
});
</script>

首先,不要混合jQuery和JavaScript。如果你要做一项特定的任务,那是这样或那样的。与代码的语法保持一致。其次,代码很好,但您必须通过将open()的第三个参数设置为false来使代码同步。如果您想保持相同的jQuery语法,请将open()转换为$.post()并登录。然后,在回调中,您可以向API发出请求。