简体   繁体   中英

xhttp send username and password to url

I need to grab information from the site example.com/something.aspx?id=1 , however this page can only be accessed if the user is logged in.

Therefore, I used the following script to login the user, then grab information from the page, however I'm wondering if the script is properly formatted.

<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>

First, don't mix jQuery and JavaScript. If you are going to do an specific task, it's one way or another. Be consistent with the syntax of your code. Second, the code is fine, but you'll have to make that code synchronous by setting the third parameter of open() to false. If you want to keep the same jQuery syntax, turn your open() into a $.post() and log in. Then, in the callback, you can make the request to your API.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM