我正在一个连接到rest-api的网站上工作。用户可以登录网页。我有一个注销按钮,在它后面我需要编写 javascript/json/http 请求来终止用户会话。这就是我所拥有的,谁能告诉我这是否有意义?
<form action="http://apiaddresshere.com" method="delete" id = "myform">
<input type = "submit" value="Logout">
</form>
<script>
var form = document.getElementById("myform");
form.onsubmit = function (e) {
// stop the regular form submission
e.preventDefault();
// construct an HTTP request
var xhr = new XMLHttpRequest();
xhr.open(form.method, form.action, true);
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
// send the null data as JSON
xhr.send(JSON.stringify());
xhr.onloadend = function () {
// done
};
};
</script>