如何在 node.js express 中使用 jsonp

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18231579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:18:34  来源:igfitidea点击:

How to use jsonp with node.js express

node.jsexpressjsonp

提问by newbeeep

i'm trying to make Samsung Smart TV app with node.js .

我正在尝试使用 node.js 制作三星智能电视应用程序。

In my project, i want to make my app communicating with server pc.

在我的项目中,我想让我的应用程序与服务器 pc 通信。

According to Many Web sites, i can do this with "jsonp".

根据许多网站,我可以用“jsonp”来做到这一点。

Here is a client side code that i found.

这是我找到的客户端代码。

<html>
<head>
    <title>jsonp test</title>
    <script src="http://code.jquery.com/jquery-1.6.2.min.js"></script>      
    <script type="text/javascript">
        $(function(){               
            $('#select_link').click(function(e){
                e.preventDefault();
                console.log('select_link clicked');

                function test(data){
                    return {"message":"ok"};
                }

                 $.ajax({
                    dataType: 'jsonp',
                    data: "data=yeah",                      
                    jsonp: 'callback',
                    url: 'http://172.20.10.3:3000/endpoint?callback=?',                     
                    success: function(data) {
                        console.log('success');
                        console.log(JSON.stringify(data));
                    }
                });
            });             
        });
    </script>
</head>
<body>
    <div id="select_div"><a href="#" id="select_link">Test</a></div>    
</body>

And, here is a server side code that i found.

而且,这是我找到的服务器端代码。

app.get('/endpoint', function(req, res){
var obj = {};
obj.title = 'title';
obj.data = 'data';

console.log('params: ' + JSON.stringify(req.params));
console.log('body: ' + JSON.stringify(req.body));
console.log('query: ' + JSON.stringify(req.query));

res.header('Content-type','application/json');
res.header('Charset','utf8');
res.send(req.query.callback + '('+ JSON.stringify(obj) + ');');
});

These codes are working on my pc(server pc), but when i open client page on others computer, it doesn't work.

这些代码在我的电脑(服务器电脑)上工作,但是当我在其他电脑上打开客户端页面时,它不起作用。

Console just give me this log :

控制台只是给我这个日志:

 X GET http://172.30.2.2:3000/endpoint?callback=jQuery11020685203080996871_1376482492523&data=yeah&_=1376482492524  

I want to use jsonp to handle cross-domain, but it doesn't work i think...

我想使用 jsonp 来处理跨域,但我认为它不起作用......

What can i do to fix this?

我能做些什么来解决这个问题?

Please give me help!!

请给我帮助!!

回答by Nur Rony

just use

只是使用

res.jsonp(obj)

you can go ExpressJS JSONPfor more info

你可以去ExpressJS JSONP获取更多信息

回答by Hymanhao

Try to replace

尝试更换

res.send(req.query.callback + '('+ JSON.stringify(obj) + ');');

res.send(req.query.callback + '('+ JSON.stringify(obj) + ');');

with

res.jsonp(req.query.callback + '('+ JSON.stringify(obj) + ');');