这与Stream data with Node.js差不多,但我觉得这个问题没有得到充分的回答.
我试图使用jQuery ajax调用(get、load、getJSON)在页面和 node 之间传输数据.js服务器.我可以从浏览器中点击地址,看到"你好,世界!"!",但当我从我的页面try 此操作时,它失败了,并显示我没有得到响应.我设置了一个简单的测试页面和hello world示例来测试此操作:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>get test</title>
</head>
<body>
<h1>Get Test</h1>
<div id="test"></div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js"></script>
<script>
$(document).ready(function() {
//alert($('h1').length);
$('#test').load('http://192.168.1.103:8124/');
//$.get('http://192.168.1.103:8124/', function(data) {
// alert(data);
//});
});
</script>
</body>
</html>
和
var http = require('http');
http.createServer(function (req, res) {
console.log('request received');
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8124);