【问题标题】:Unexpectedly blank Ajax response text意外空白的 Ajax 响应文本
【发布时间】:2015-09-29 14:01:51
【问题描述】:

我尝试使用 Node.js 测试我的 Ajax 代码。我以前没用过,所以我是新手。 这是我的代码:

<script>
  var xmlHttp = false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {
        xmlHttp = false;
      }
    }
    @end @*/

  if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
    xmlHttp = new XMLHttpRequest();
  }

  function callServer() {
    var url = "http://localhost:8888/";
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = updatePage;
    xmlHttp.send(null);
  }

  function updatePage() {
    if (xmlHttp.readyState == 4) {
      var response = xmlHttp.responseText;
      alert(response);
    }
  }
</script>

还有我的 Node.js 服务器:

var http = require("http");

http.createServer(function(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Test");
  response.end("Test");
}).listen(8888);

我运行带有属性的脚本:onclick="callServer()"。警报什么也没告诉我。谢谢。

【问题讨论】:

  • 你确定你跑过节点吗? node server 之类的东西必须使用终端或同等设备发送。
  • @KJPrice 我敢肯定。我在文件末尾和函数(请求,响应)的开头添加了 console.log。
  • 我创建了一个测试服务器,我看到了No 'Access-Control-Allow-Origin' header is present on the requested resource,这个出现在你的console.log上了吗?
  • @fuyushimoya 否。我使用 console.log 编辑的代码输出如下: node test.js 服务器已启动 收到请求(单击按钮后)

标签: javascript html ajax node.js


【解决方案1】:

试试这些改变:

  1. 改变

xmlhttp.send(null);xmlhttp.send();

  1. 改变

var xmlHttp = false;var xmlHttp = null;

  1. if else 改为如下:

if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');}

  1. 下面看起来多余,你可以评论一下:
    // response.write("Test");

  2. 确保节点正在运行。

祝你好运。

【讨论】:

  • 没有帮助,很遗憾。
  • 试试这个改变: from: response.writeHead(200, {"Content-Type": "text/plain"}); to: response.writeHead(200, {'Content-Type': 'text/html'});
【解决方案2】:

您的代码工作正常,问题是您在不同的域或端口上运行您的客户端,然后您的服务器运行。

尝试在http://localhost:8888/ 上运行您的客户端。

编辑:

使用 Express 包非常简单:

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

app.get('/', function(req, res) {
  res.send('Test');
});

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

app.listen(8888, function() {
  console.log('Server started at port 8888');
});

编辑#2

没有快递:

var http = require('http'),
    fs   = require('fs');    

var server = http.createServer(function(req, res) {
  fs.readFile('./index.html', function(err, data) {
    res.writeHeader(200, { 'Content-Type': 'text/html' });
    res.write(data);
    res.end();
  });
}).listen(8888);

server.on('request', function(req, res) {
  if(req.url == '/test') {
    res.writeHeader(200, { 'Content-Type': 'text/plain' });
    res.write('Test');
    res.end();
  }
});

编辑#3

要提供静态资产,请使用 express.static 中间件,如下所示:

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

app.use(express.static(__dirname + '/public'));

您应该将您的资产放在根目录下的public 目录中。

【讨论】:

  • 如何在 localhost:8888 中运行我的客户端?我没有在任何地方部署我的客户端。我的意思是,我把所有东西都放在我的电脑上,然后在上面运行 .html 文件。
  • 此代码有效,非常感谢。但是有没有不使用 Express 的方法?
  • 因为我用 express 丢失了我的 js、css 和字体。
猜你喜欢
  • 2015-07-01
  • 2012-08-28
  • 1970-01-01
  • 2013-11-22
  • 1970-01-01
  • 1970-01-01
  • 2016-03-27
  • 2023-04-06
  • 2018-04-10
相关资源
最近更新 更多