小编bri*_*nad的帖子

如何在node.js中使用jQuery ajax调用

这与使用Node.js的Stream数据类似,但我觉得问题得不到充分回答.

我正在尝试使用jQuery ajax调用(get,load,getJSON)在页面和node.js服务器之间传输数据.我可以从我的浏览器中查看地址并查看"Hello World!",但是当我从我的页面尝试此操作时,它会失败并显示我没有得到任何响应.我设置了一个简单的测试页面和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>
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

html javascript ajax jquery node.js

70
推荐指数
2
解决办法
10万
查看次数

标签 统计

ajax ×1

html ×1

javascript ×1

jquery ×1

node.js ×1