Super Simple Ajax (Without a Framework)

Published Friday 12th of June 2009

If you're looking for the bare minimum when it comes to doing cross-browser GET or POST XHR then this might be the script for you.

I know there's hundreds (probably thousands) of these scripts out there already but even the most lightweight ones I could find seemed kind of bloated.

Example

// GET latest-comments.php and update the #latest-comments element
superSimpleAjax({url'latest-comments.php'}, 'latest-comments');

// GET article with ID 12 and alert its contents
superSimpleAjax({
    
url:        'get-article.php'
    
data:        'id=12'
    
callback:    function (data) {
        
alert(data);
    }
});

// POST a comment to post-comment.php
superSimpleAjax({
    
method:        'POST'
    
url:        'post-comment.php'
    
data:        'author=John Doe&email=johndoe@johndoe.com&content=Hi, my name is John Doe'
    
callback:    function (data) {
        
alert('Thanks for your comment!');
    }
});

Grab the code from Google Code. It's about 1k uncompressed.

Enjoy!

Tags
Comments
No comments

Bookmark this Article

  • del.icio.us
  • Digg
  • Furl
  • Google
  • Technorati
  • Ma.gnolia
  • Blinklist
  • Blogmarks
  • Rojo
  • Stumbleupon
blog comments powered by Disqus

Random jQuery Plug-ins

  • Ajax Loader

    Use this plug-in when you want to inform your visitors that a certain part of your page is currently loading. The plug-in adds a faded 'loading-div' o...

    Details

  • Togglable DL

    Use this plug-in on your definition-lists (dl-elements) to allow the dd:s for a dt to be toggled when clicking the dt. Don't use it unless it's semant...

    Details

  • Captcha Refresh

    If you use a so called CAPTCHA-image on your site then you can use this plug-in to allow users to click your CAPTCHA in order to generate a new random...

    Details

More Plug-ins

Recent Comments

Powered by Disqus
Page cached. Loaded in: 0.0442 second(s).
Last DB change: 2012-04-02 11:06:05
Last file change: 2012-04-25 20:30:39
Cache created: 2012-05-18 07:26:41