Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use jsonp with node.js express

Tags:

People also ask

How do I use JSONP?

Method to use JSONP:In the URL include the callback parameter in the end. When the browser comes across the script element, it sends HTTP request to the source URL. The server sends back the response with JSON wrapped in a function call.

Can JSONP execute JavaScript?

JSONP enables sharing of data bypassing same-origin policy, which disallows running JavaScript code to read media DOM elements or XMLHttpRequest data fetched from outside the page's originating site.

What is difference between JSON and JSONP?

Json is stardard format that is human readable used to transmit information from one server to another server. Jsonp is a json with ability to transmit information to another domain. JSONP is JSON with padding, that is, you put a string at the beginning and a pair of parenthesis around it.

What is RES JSONP?

The res. jsonp() function is used to send a JSON response with JSONP support and this function is similar to the res. json() function except that it opts-in to the support of JSONP callback. Syntax: res.jsonp( [body] ) Parameter: The body parameter describes the body type which can be sent in response.


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

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

According to Many Web sites, i can do this with "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...

What can i do to fix this?

Please give me help!!