How to read data from json that come from diffrent domain – cross domain solution – jsonp

In a situation where you develop app for mobile that all the files will be on the smrtphone and you want to read data from remote server(diffrent domain – you on the local,and the remote is someting else).

this solution call “jsonp”.
and it mean that you wrapped the json code in the server with function, and you call that function from your mobile device, and you must have also the same function name in your mobile so it will catch the data from the function in the server .

example :
on the local files(maybe mobile app) i have code :


<script type="text/javascript">
                function update(data)
                {
                    //the data is what i get from the json file on remote server
                }
</script>

to call the function we need to put at the end of the file(local)


<script type="text/javascript"
            src="https://blabla-c9-a1_shay.c9.io/jsonp.json">
</script>

and the json file on the remote server diffrent domain : (i Recommend c9.io for testing)


update(
{
    "articles":[
        {
            "id"        : 1,
            "name"      : "baba",
            "content"   : "baba is alway something",
            "id"        : "001122",
            "city"      : "tel aviv",
            "bigpic"    : "https://blabla-c9-a1_shay.c9.io/home-big.png",
            "smallpic"  : "https://blabla-c9-a1_shay.c9.io/home-small.png",
            "video"     : "mp4Encode.mp4"
        },
        {
            "id"        : 2,
            "name"      : "gal",
            "content"   : "gal somking sigar",
            "id"        : "334455",
            "city"      : "yavne",
            "bigpic"    : "https://blabla-c9-a1_shay.c9.io/tree-big.png",
            "smallpic"  : "https://blabla-c9-a1_shay.c9.io/tree-small.png",
            "video"     : "https://blabla-c9-a1_shay.c9.io/homeVid.mp4"
        }
    ]
});
Advertisements
This entry was posted in כללי and tagged , . Bookmark the permalink.

One Response to How to read data from json that come from diffrent domain – cross domain solution – jsonp

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s