加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何使用jsonp与node.js express

发布时间:2020-12-16 19:49:08 所属栏目:百科 来源:网络整理
导读:我正在尝试使用node.js制作三星智能电视应用程序. 在我的项目中,我想让我的应用程序与服务器电脑通信. 根据许多网站,我可以用“jsonp”来做到这一点. 这是我发现的客户端代码. htmlhead titlejsonp test/title script src="http://code.jquery.com/jquery-1.
我正在尝试使用node.js制作三星智能电视应用程序.

在我的项目中,我想让我的应用程序与服务器电脑通信.

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

这是我发现的客户端代码.

<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>

而且,这里是我发现的服务器端代码.

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) + ');');
});

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

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

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

我想使用jsonp处理跨域,但它不工作,我想…

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

请给我帮助!

只是使用
res.jsonp(obj)

你可以去ExpressJS JSONP了解更多信息

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读