SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)

    AJAX - What is it and how do you use it?

    Hello,

    Okay, I have a simple, and fairly stupid question: What is AJAX?

    Now, I know AJAX stands for Asynchronous JavaScript and XML and it uses something called the XMLHttpRequest object. I also know it's not a language in itself, but merely a "fancier" version of Javascript.

    So, basically, some questions I have:
    1) How much does it differ from regular Javascript?
    2) Is it completely client-side, or are there server-side components?
    3) Is it standardized, or are their multiple implementations?
    4) Are there any dependencies that have to be put on the site, or are they all in the browser?
    5) How compatible is AJAX, and how gracefully can it degrade (i.e. can it fall back to simple Javascript if AJAX isn't usable)?

    Thanks. Any other info or learning resources you want to supply, please supply them.

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,751
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1) How much does it differ from regular Javascript?
    Well, I'm not sure about differ, it is regular javascript, it's just acting on a browser implemented object.

    2) Is it completely client-side, or are there server-side components?
    There are generally server side components. This can be another website, or web services hosted on your own site.

    There's 2 popular ways you can get an AJAX response back, JSON (JavaScript Object Notation), which is fairly easy to parse into a readily available object, or XML, which takes a bit more effort to deal with.

    3) Is it standardized, or are their multiple implementations?
    IE implemented the XHttp object as an ActiveX object (Microsoft.XMLHTTP to be exact). Other browsers use the XHttpRequest object. However, the actual calls are fairly consistent across the implementation (callbacks, sending/getting data etc). One major area where ie differs is that it has this annoying caching thing going on. This causes bizzare debugging issues.

    4) Are there any dependencies that have to be put on the site, or are they all in the browser?
    Running a recent browser and javascript enabled really. Also with the XHttp object being an Active X control, user security preferences might get in the way.

    5) How compatible is AJAX, and how gracefully can it degrade (i.e. can it fall back to simple Javascript if AJAX isn't usable)?
    You can do try/catch statements when assign or dealing with your calls, then act upon failure accordingly.

  3. #3
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by samanime View Post
    1) How much does it differ from regular Javascript?
    The client-side part of an ajax application is written in regular JavaScript.

    Quote Originally Posted by samanime View Post
    2) Is it completely client-side, or are there server-side components?
    No, there's a client-side part and a server-side part. The client-side part, written in JavaScript, calls the server-side application, which responds with (usually) an XML document. The JavaScript code parses the response and updates the page in the browser, without requiring the page to be reloaded.

    Quote Originally Posted by samanime View Post
    3) Is it standardized, or are their multiple implementations?
    IE5 and IE6 implement XMLHttpRequest as an ActiveX component. IE7, like Opera, Firefox, Safari et al implement it as a real JavaScript object. The main difference is how you instantiate the XMLHttpRequest object. After that it's pretty much the same. Any decent JavaScript library will provide a single interface for this.

    Quote Originally Posted by samanime View Post
    4) Are there any dependencies that have to be put on the site, or are they all in the browser?
    Not sure what you mean here, but you have to have something on the server that responds to the HTTP request. For security reasons, XMLHttpRequest can only call on pages that reside on the same domain as the page containing the JavaScript.

    Quote Originally Posted by samanime View Post
    5) How compatible is AJAX, and how gracefully can it degrade (i.e. can it fall back to simple Javascript if AJAX isn't usable)?
    It's fairly compatible (except from the object instantiation, as mentioned above). The fallback should be a normal HTTP request; i.e., reloading the page the normal way.
    Birnam wood is come to Dunsinane

  4. #4
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    Okay, so, the server-side component is basically just any files that I want to use, meaning it doesn't need some kind of interpreter like PHP does, correct?

    And, by dependencies, I meant something like needing something special installed on the server, or some extra js files that need to be available on the website for AJAX to work.

    Thanks for the responses everyone.

  5. #5
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  6. #6
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,751
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, so, the server-side component is basically just any files that I want to use, meaning it doesn't need some kind of interpreter like PHP does, correct?
    Yes, as long as it's on the same domain and subdomain as the script (for security reasons). However, most AJAX setups will have the server-side component as some kind of dynamic script like PHP, Ruby, Python, whatever you want.

    And, by dependencies, I meant something like needing something special installed on the server, or some extra js files that need to be available on the website for AJAX to work.
    You'll need a script to setup the ajax calls and what not, or you can use one of the many prebuilt libraries such as jquery, prototype and yui to handle ajax transparently for you.

  7. #7
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by samanime View Post
    Okay, so, the server-side component is basically just any files that I want to use, meaning it doesn't need some kind of interpreter like PHP does, correct?
    It's possible, but it wouldn't be very useful. You'd normally want a server-side script that accepts some parameters you pass with the XMLHttpRequest, does some sort of computing and returns a response that depends on those parameters.
    Birnam wood is come to Dunsinane

  8. #8
    Coding and Breathing CoderMaya's Avatar
    Join Date
    Feb 2008
    Location
    Atlit, Israel
    Posts
    470
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would just like to say that AJAX is not a 'fancier version of Javascript' or 'different from Javascript'. It's merely a new understanding of an old built in Javascript object called XMLHTTPRequest, which lets you send requests to the webserver via Javascript and thus, without actually reloading the page or launching a new page.

  9. #9
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    So, basically, I can use PHP and what-not like I would always use, and AJAX lets me call them without doing a full refresh of the page, right? That's the basic idea.

    By "interpreter like PHP does", I meant strictly that there isn't any 'AJAX interpreter', not that there wouldn't have any kind of server-side functionality available.

    Now that I think about it, do the Sitepoint forums use AJAX, or something similar? Since it let's you edit and add posts without having to fully reload the page.

  10. #10
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by samanime View Post
    By "interpreter like PHP does", I meant strictly that there isn't any 'AJAX interpreter', not that there wouldn't have any kind of server-side functionality available.
    The PHP script doesn't necessarily know that it's being called through ajax. It just receives a normal HTTP GET or POST request and responds the normal way.

    Quote Originally Posted by samanime View Post
    Now that I think about it, do the Sitepoint forums use AJAX, or something similar? Since it let's you edit and add posts without having to fully reload the page.
    That's probably done with ajax, as is the auto-completion of usernames when you compose a new PM.
    Birnam wood is come to Dunsinane


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •