Cross-domain AJAX calls

Share

In these days all web2.0 apps make use of AJAX to save bandwidth, user time or to display data without refreshing the page. When you make an AJAX request the response could be plain text, Html or JSON, but if you want to make a cross-domain AJAX call you can’t because the browser security model dictates that XMLHttpRequest must have the same domain in order to communicate. This is where you need to use JSONP, this technique will make the AJAX call and after will inject the response in a script tag and will pass the data to a specified function.

Some JavaScript Libraries help you dealing with JSONP and in this article I will show you how to make cross-domain AJAX calls with MooTools and jQuery.

MooTools

If you want to make JSONP requests you need the plugin Request.JSONP from MooTools More.

var myRequest = new Request.JSONP({
	url: 'http://twitter.com/statuses/user_timeline/twitter.json',
	onComplete: function(data) {
		//do something with data
	}
}).send();

MooTools will automatically add callback parameter with function which will handle the response, but if the server requires another parameter for callback you can add the callBackKey option. If you need to pass data to request you need to add the values in data option.

var myRequest = new Request.JSONP({
	url: 'http://domain.com/getData',
	callBackKey: 'callback',
	data: {
		someData: 'someValue'
	},
	onComplete: function(data) {
		//do something with data
	}
}).send();

jQuery

If you worked before with $.ajax there are no big difference between a request for HTML/JSON and JSONP calls, you need just to change dataType to jsonp and to specify the callback parameter in jsonp option.

$.ajax({
	url: 'http://twitter.com/statuses/user_timeline/twitter.json&callback=?',
	dataType: 'jsonp',
	jsonp: 'callback',
	success: function(data){
		//do something with data
	}
});

You can read more about JSONP here.

Finally, there’s another very important peculiarity of what does Cialis that brings it so high above its alternatives. It is the only med that is available in two versions – one intended for use on as-needed basis and one intended for daily use. As you might know, Viagra and Levitra only come in the latter of these two forms and should be consumed shortly before expected sexual activity to ensure best effect. Daily Cialis, in its turn, contains low doses of Tadalafil, which allows to build its concentration up in your system gradually over time and maintain it on acceptable levels, which, consequently, makes it possible for you to enjoy sex at any moment without having to time it.

7 thoughts on “Cross-domain AJAX calls”
  • Nice article, nice blog, I have

    bookmarked your blog, it is worthy doing this. Thank you.

    June 9, 2010 at 6:50 am
  • Nice post…Thank you for sharing some good things!!

    June 15, 2010 at 12:16 am
  • It’s a nice blog,I like it

    June 16, 2010 at 3:33 pm
  • Enjoyed reading

    the report – most informative thanks

    July 29, 2010 at 8:10 am
  • good share, great article, very usefull for us…thanks!

    July 30, 2010 at 6:52 pm
  • Good article Thank you so much

    July 31, 2010 at 6:49 am
  • SRW says:

    Keep up the great work!I like blog.ropardo.ro !

    December 6, 2010 at 6:09 am

Comments are closed.

By continuing to use the site, you agree to the use of cookies. More information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close