代码不发送HTTP请求

时间:2017-11-24 11:09:52

标签: javascript html http xmlhttprequest

当用户按下按钮"发送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请求。这段代码有什么问题?

0 个答案:

没有答案