Last year in Feb I heard about JSONP and really liked the JSONP solutions. JSONP allows you to make an HTTP request outside your own domain
JSONP consume the Web Services from JavaScript code. Earlier I worked on so on AJAX but there is issue with working or communication issue with cross domain sites.
XMLHttpRequest is blocked from making external requests.

JavaScript code to make a JSONP call will as follows:

function common_jsonpRequest(url, name, query) {

 if (url.indexOf("?") > -1)
 url += "&jsonp="
 else
 url += name + "&";
 if (query)
 url += encodeURIComponent(query) + "&";
 url += new Date().getTime().toString(); // prevent caching

 var JSONPscript = document.createElement("script");
 JSONPscript.id = 'wordpressapi_jsonpRequest';
 JSONPscript.setAttribute("src", url + "&CacheBuster=" + Math.random());
 JSONPscript.setAttribute("type","text/javascript");

 // write the jsonp script tag
 document.body.appendChild(JSONPscript);
 //script.text = "";

}

function returncall (data){
 alert(data);
// you can use data as a variable also.
}

In same javascript you should write the returning function as above.

You can call JSONP this function as follows;

<a href="http://wordpressapi.com/files/JSONP-request.gif"><img class="alignnone size-medium wp-image-1167" title="JSONP-request" src="http://wordpressapi.com/files/JSONP-request-300x125.gif" alt="" width="300" height="125" /></a>
common_jsonpRequest(base_url+'getData.php?jsonp=mycallback&name=for_wordpressapi');

Notel: getData.php file will beahave like externaal javascript file so handle this file carefully.
getData.php sample file.

<?php
$data = "this is our dynamic data";
if($_REQUEST['name']=='for_wordpressapi'){

echo "returncall(".$data.")";

}

?> 

This will return the "this is our dynamic data" as a alert.

You might also like

How to check iframes are loaded completely in browser
Google app store will rock
Execute JavaScript functions in innerHTML
How to add event to all image elements using javascript
How to use document.write and innerhtml

10 Responses to “JSONP for cross domain communication solution using with javascript and PHP”

  1. very cool & good tip, thank you very much for sharing

  2. This article is very important to me, and I like it very much, I will add it to my favorites.

  3. this is a really good resource for educators. thanks for sharing.

  4. Andre Dan says:

    I don’t know why I clicked on the link to your blog, but I’m glad I did. Fine and dandy post! Please keep writing more.

  5. Eco Tours says:

    Exceptional webpage. My class mates and I were just discussing this the other night. Also your webpage looks great on my old laptop. Now thats uncommon. Nice work.

  6. Super-Duper site! I am loving it!! Will come back again – taking you feeds also, Appreciation.

  7. You made some good points there. I did a search on the topic and found most people will agree with

  8. Wow dude, this is really helpful information, much appreciated.

  9. In one moment in my search, I find your site, and always return for here because enjoy it.

  10. I don’t normally post to blogs but I enjoyed this post so keep up the good work.

Leave a Reply

Wordpressapi is developer code book.
wordpressapi on Facebook
© 2010 Wordpressapi. All Rights Reserved. Reproduction without explicit permission is prohibited.