北城已荒凉 2015-11-17 04:51 采纳率: 0%
浏览 19

为什么我得到空结果?

I'm learning JavaScript & Ajax & NodeJs.

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. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. Ajax1.html:28 State: 4 status: 0 Ajax1.html:35 Got Result:

Why I'm getting an empty string ? what am I missing ? Thanks

  • 写回答

1条回答 默认 最新

  • weixin_33749242 2015-11-17 05:06
    关注

    Your client code and server code must be on the same origin (same hostname, same port) for you to make AJAX requests.

    127.0.0.1 cannot send AJAX requests to 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.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?