forked from simplicitesoftware/ajax-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic-xhr-example.html
31 lines (31 loc) · 1.07 KB
/
basic-xhr-example.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="shortcut icon" href="favicon.ico"/>
<title>Ajax call on REST service example</title>
<style type="text/css">
body { font-family: Helvetica, Arial, sans-serif; }
</style>
<script type="text/javascript">
window.onload = function() {
var username = "website";
var password = "simplicite";
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.open("POST", "https://demo.dev.simplicite.io/api/rest/", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.setRequestHeader("X-Simplicite-Authorization", "Basic " + btoa(username + ":" + password));
xhr.send();
xhr.onerror = function(e) { console.log("Error: " + e.target.status); };
xhr.onload = function() {
var res = JSON.parse(xhr.responseText);
document.getElementById("main").innerHTML = "<p>Hello " + res.grant.login + "!</p>";
};
}
</script>
</head>
<body>
<div id="main"></div>
</body>