我嘗試使用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