简体   繁体   English

XMLhttpRequest与节点js服务器一起使用express

[英]XMLhttpRequest with a node js server with express

for a reason that i don't know i can't use the xmlhttprequest in my local server created in node js and express. 由于我不知道无法在节点js和express中创建的本地服务器中使用xmlhttprequest的原因。 Let me show you... Here is the server in node JS: 让我向您展示...这是节点JS中的服务器:

var 
express = require('express'),
app = express.createServer();

app.get('/count', function(req, res) {
    res.type('text/plain');
    res.write('Hello!')
     res.end();
});

app.listen(4003);

And it runs jus fine! 它运行得很好! when i acess localhost:4003/count i see "Hello!". 当我访问本地主机:4003 / count时,我看到“你好!”。

Now that the server is OK, let's check the html page: 现在服务器正常,让我们检查html页面:

<script>

var xmlhttp = new XMLHttpRequest();
var resp = ""
xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200){
        resp = xmlhttp.responseText;
        }
}

var cam = "localhost:4003/count"


function show(){
    xmlhttp.open("GET",cam,  true);
    xmlhttp.send();
    alert(resp);
}

</script>

<center><h1><div style="color:#339966"> <input type="button" value="Return of /count" onclick="javascript:show()"> </div> </h1>

So, it just don't work =[, i've already tried to: 因此,它只是不起作用= [,我已经尝试过:

  • change localhost:4003/count as value of the variable cam , for http://localhost:4003/count , 127.0.0.1:4003/count . 更改localhost:4003/count作为变量cam值,对于http://localhost:4003/count127.0.0.1:4003/count http://localhost:4003/count
  • Open in Firefox, Safari and Chrome. 在Firefox,Safari和Chrome中打开。
  • When i try to see the xmlhttp.status with an alert() it shows ' 0 '. 当我尝试查看带有alert()xmlhttp.status ,它显示为“ 0 ”。 (but the page open normally in any browser) (但该页面可以在任何浏览器中正常打开)

Along with the timing issues that Morgan already covered ... 以及摩根已经涵盖的时间问题...

  • When including a host in a URL, it needs to be prefixed by // . 在URL中包含主机时,需要以//为前缀。

     var cam = "//localhost:4003/count"; 

    Otherwise, it'll be treated as a relative path with localhost:4003 as a directory name. 否则,它将被视为相对路径,以localhost:4003作为目录名称。

  • You also have to deal with the Same-Origin Policy when using Ajax. 使用Ajax时,还必须处理Same-Origin策略

    So, if you're trying to use file:// for the page, this won't typically work well. 因此,如果您尝试将file://用于页面,这通常不会很好。 The FILE protocol doesn't have an origin, so it can't pass the SOP, resulting in errors similar to: FILE协议没有原点,因此无法传递SOP,导致类似于以下错误:

     Origin null is not allowed by Access-Control-Allow-Origin. 

    The easiest solution is to also serve the page from the application. 最简单的解决方案是从应用程序中提供页面。

    You can do so either with a static() middleware . 您可以使用static()中间件来执行此操作

     // `GET /` will serve `./public/index.html` app.use(express.static(__dirname + '/public')); 

    Or with another route that sends the file itself. 或者使用另一个发送文件本身的路由。

     app.get('/', function (req, res) { res.sendfile(__dirname + '/public/index.html'); }); 

    Then, access the page from: 然后,从以下位置访问该页面:

     http://localhost:4003/ 
  • Also, with both the page and the count now served from the same origin, the URL can simply be relative-from-root: 此外,由于页面和count现在都来自同一个来源,因此URL可以简单地相对于root:

     var cam = "/count"; 

At the time you are trying to alert(resp) the resp variable hasn't been assigned, because onreadystatechange has not been called. 在您尝试alert(resp) ,尚未分配resp变量,因为尚未调用onreadystatechange If you moved the alert into onreadystatechange you should get the desired effect. 如果您将警报移动到onreadystatechange您应该获得所需的效果。 This is also the point you could assign that value to an Element in the page. 这也是您可以将该值分配给页面中的Element的点。

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

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