我已經用Node.js的概念測試了Hello world程序,它工作正常。如何在Node.js中集成ajax?
文件的詳細信息:
的index.html
socket.js
在命令提示符下運行:節點socket.js
和我有同樣的使用嘗試Ajax調用中的node.js打招呼世界文件,並按照下面的鏈接代碼,
how to use jQuery ajax calls with node.js
個server.js
var http = require('http');
http.createServer(function (req, res) {
console.log('request received');
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('_testcb(\'{"message": "Hello world!"}\')');
}).listen(8080);
的index.html
<script src="/socket.io/socket.io.js"></script>
<script>
$(document).ready(function() {
$.ajax({
url: 'http://localhost:8080/',
dataType: "jsonp",
jsonpCallback: "_testcb",
cache: false,
timeout: 5000,
success: function(data) {
$("#test").append(data);
},
error: function(jqXHR, textStatus, errorThrown) {
alert('error ' + textStatus + " " + errorThrown);
}
});
});
</script>
<div id="test"></div>
,我和在命令提示下述方法運行
節點的Node.js
而在像瀏覽器中運行,
然後,它提供了以下輸出
_testcb(「{‘消息’:‘世界,你好’}」)
,但我不能得到正確的結果,請給我解釋一下,如何整合帶有示例代碼的node.js中的ajax?
感謝您的幫助。
你可以粘貼當前的代碼嗎?那麼我們可以有一個更好的問題。 – footy
我有以下代碼從這個鏈接http://stackoverflow.com/questions/5373987/how-to-use-jquery-ajax-calls-with-node-js/5376131#5376131 – Sivanath
我ment,代碼*你*已經做到現在。 'index.html'和'socket.js'文件中的詳細信息 – footy