当用户按下按钮"发送JSON"时,我想从HTML页面发送HTTP请求。客户端实现如下:
<html>
<head>
<title>Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>Page</div>
<p>Click the button below to send json</p>
<button type="button" onclick="sendJson()">Send file</button>
<p id="demo"></p>
<script>
function sendJson()
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "http://localhost:9000/test");
xmlhttp.setRequestHeader("Content-Type", "application/json");
var data = JSON.stringify({"email": "hey@mail.com", "password": "101010"});
xmlhttp.send(data);
}
</script>
</body> </html>
http://localhost:9000/test是服务器运行的URL。
此代码的问题在于它不会发送HTTP请求。这段代码有什么问题?