【问题标题】:Post to nodejs and return results发布到 nodejs 并返回结果
【发布时间】:2011-12-02 16:56:40
【问题描述】:

我已经在谷歌上搜索了几个小时,以便从我的网站上托管的客户端 js 发布到 nodejs 服务器,而不是将这些结果返回给我的客户端。

这是我想要完成的一个例子:

<html>
<head>
<script type="text/javascript">
var my_id = <?=$myuserid;?>;
$(function(){
    $.ajax({
        url: 'http://mydomain.com:8001/get_friends',
        dataType: 'JSONP',
        type: 'POST',
        crossDomain: true,
        data: {
            id: my_id
        },
        success: function(data){
            if(data.error===true){
                alert(data.msg);
            } else {
                alert(data.users);
            }
        }
    });
});
</script>
</head>
<body>
</body>
</html>

比 NodeJS 文件:

var http = require('http'),
    url = require('url'),
    mysql = require('mysql'),
    sys = require('sys');
var client = mysql.createClient({
    user: 'root',
    password: 'password',
});

http.createServer(function(request, response){
    client.query('USE mydatabase');
    client.query(
        'SELECT * FROM buddy_list WHERE user_id = "This is where I need my post to go"',
        function selectCb(err, results, fields){
            if(err){
                throw err;
            }
            response.writeHead(200, { 'Content-Type' : 'application/json' });
            "Here I need it to feed back results to client script."
            response.end();
            client.end();
    })
}).listen(8001);

【问题讨论】:

    标签: node.js cross-domain jsonp jquery-post


    【解决方案1】:
    response.writeHead(200, { 'Content-Type' : 'application/json' });
    response.send( JSON.stringify(results) );
    

    我还没有尝试过,但它看起来像你想要的。我不确定有什么结果、字段或者是否需要更多的数据操作。我假设“结果”是一个 JSON 对象。

    【讨论】:

    • 为什么不只是res.json(results)
    • res.json 和 res.send 是 Express 原生调用,在 Node 核心中无法识别。
    【解决方案2】:

    抛开输入卫生问题不谈,以下是解决此问题的方法:

    http.createServer(function(request, response){
        request.on('data', function(chunk) {
            dataInput = chunk.toString();
            client.query('USE mydatabase');
            client.query(
                'SELECT * FROM buddy_list WHERE user_id = ' + dataInput,
                function selectCb(err, results, fields){
                    if(err){
                        throw err;
                    }
                    response.writeHead(200, { 'Content-Type' : 'application/json' });
                    "Here I need it to feed back results to client script."
                    response.end();
                    client.end();
            })
    
        })
    }).listen(8001);
    

    【讨论】:

    • 是否可以在客户端服务器上接收这些结果?
    • 我没有使用 nodejs 的 mysql 连接器的经验,但我猜你可以用 response.end({'results': results}); 替换 response.end(); 行。我的建议是在代码中添加 console.log 语句,以便更好地了解正在发生的事情以及这些事情发生的时间以及数据最终采用的格式。但是,当您有数据要发送回客户端时, 将其作为唯一参数插入到 response.end(argument) 中。
    • 抱歉,我不能编辑上面的评论,但请将 response.end({'results': results}); 替换为 response.end(JSON.stringify({'results': results}));。我的错。
    • 如果我直接发布到脚本,我能够让它显示结果,但出于某种原因,从我的客户端进行 ajax 发布不会返回任何结果。这只是我一直试图弄清楚的一点。其他一切都很好。
    • 这是假设块不是很大,因此必须在第一次调用后完成。如果你有一个大的 Json 文件怎么办?您必须使用req.on('end', func ... ) 来解决问题并在末尾发送回复res.end()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    • 2015-07-07
    • 2011-11-07
    相关资源
    最近更新 更多