我尝试使用Node.js测试我的Ajax代码。我以前没有用过,所以我是新手。 这里是我的代码:意外空白的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()”。而警报什么都没有显示。谢谢。
你确定你运行了节点吗?像“节点服务器”这样的东西必须使用终端或等价物发送。 –
@KJPrice我很确定。我在文件末尾和函数(request,response)的开头添加了console.log。 –
我创建了一个测试服务器,并且在请求的资源中看到'No'Access-Control-Allow-Origin'标头,这是否显示在您的console.log文件中? – fuyushimoya