[英]How to call on json based API
Im trying to call on a json based authentication API, but its not working. 我试图调用基于json的身份验证API,但无法正常工作。 The API takes two parameters as input in JSON format; 该API接受两个参数作为JSON格式的输入; username and password. 用户名和密码。
What am I doing wrong? 我究竟做错了什么? here is my current test code: 这是我当前的测试代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>API test</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
<h1>API test</h1>
<button id="btn">test</button>
</header>
<p id="result"></p>
<script>
const xhr = new XMLHttpRequest();
xhr.onload = function () {
const serverResponse = document.getElementById("result");
serverResponse.innerHTML = this.responseText;
}
xhr.open("POST", "url");
xhr.setRequestHeader("Content-type", "application/json");
xhr.send("username=test&password=test");
</script>
</body>
</html>
Try this one. 试试这个。
xhr.open("POST", "url");
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.send(JSON.stringify({ username: "test", password: "test" }));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.