简体   繁体   English

为什么我得到空结果?

[英]Why I'm getting empty result?

I'm learning JavaScript & Ajax & NodeJs. 我正在学习JavaScript,Ajax和NodeJ。

I'm trying to build a web page which display a string from server. 我正在尝试构建一个显示服务器字符串的网页。

The server code: 服务器代码:

var express = require('express');
var app = express();

app.get('/sms', function (req, res) {
   console.log("Got request: " + req);
   res.send('Hello World');
   console.log("Send result");
})

var server = app.listen(8081, function () {

  var host = server.address().address
  var port = server.address().port

  console.log("Listening at http://%s:%s", host, port)

})

The client code: 客户端代码:

<html>
<head>
    <title> Test AJAX </title>
</head>
<body>      

     <p>    
        <button onclick="sendRequestForSms()"> Get SMS From Server </button>
        </br>
        <h1 id="smsId"> SMS: <h1>
     </p>

    <script type="text/javascript">

            var xmlhttp;
            if (window.XMLHttpRequest) 
            {
                xmlhttp = new XMLHttpRequest();
            }
            else 
            {
                // code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }

            xmlhttp.onreadystatechange = function() 
            {       
                  console.log('State: ' + xmlhttp.readyState + ' status: ' + xmlhttp.status);           
                  // 1: server connection established
                  // 4: request finished and response is ready
                  // 200: "OK"
                  // 404: Page not found
                  if (xmlhttp.readyState == 4)// && xmlhttp.status == 200) 
                  {
                        console.log('Got Result: ' + xmlhttp.responseText);
                        document.getElementById("smsId").innerHTML = xmlhttp.responseText;//"done";
                  }
            };

            function sendRequestForSms() {
                xmlhttp.open("GET", "http://127.0.0.1:8081/sms", true);
                xmlhttp.send();     
            }                                       
        </script>

</body>

</html>

After I'm clicking on the button I'm getting an empty string. 单击按钮后,我得到一个空字符串。

Here the log: State: 1 status: 0 Ajax1.html:1 XMLHttpRequest cannot load http://127.0.0.1:8081/sms . 此处的日志:状态:1状态:0 Ajax1.html:1 XMLHttpRequest无法加载http://127.0.0.1:8081/sms No 'Access-Control-Allow-Origin' header is present on the requested resource. 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin 'null' is therefore not allowed access. 因此,不允许访问原始“空”。 Ajax1.html:28 State: 4 status: 0 Ajax1.html:35 Got Result: Ajax1.html:28状态:4状态:0 Ajax1.html:35得到结果:

Why I'm getting an empty string ? 为什么我得到一个空字符串? what am I missing ? 我想念什么? Thanks 谢谢

Your client code and server code must be on the same origin (same hostname, same port) for you to make AJAX requests. 您的客户端代码和服务器代码必须位于相同的来源(相同的主机名,相同的端口),才能发出AJAX请求。

127.0.0.1 cannot send AJAX requests to 127.0.0.1:8081. 127.0.0.1 无法将AJAX请求发送到127.0.0.1:8081。

To fix, either reverse proxy your node.js to the same hostname (recommended), or set an Access-Control-Allow-Origin in node.js. 要解决此问题,可以将node.js反向代理到相同的主机名(推荐),或者在node.js中设置Access-Control-Allow-Origin。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM