Is this state-of-the-art Ajax/jQuery, or should I do it differently?

Posted by AP257 on Stack Overflow See other posts from Stack Overflow or by AP257
Published on 2011-01-04T12:45:16Z Indexed on 2011/01/04 12:54 UTC
Read the original article Hit count: 199

Filed under:
|

Hi everyone:

I'm using Ajax and jQuery to do some cool client-side stuff. Basically, if the user chooses a subject from a from a drop-down list, I want to auto-populate the page with a set of book details.

However, I've built the Ajax from Google results and bits of string. I don't know if what I've done is up-to-date, or whether there are now much neater ways of doing it!

Here's what I have: can it be improved?

    $("#subjectlist").change(function() { 
        if (window.XMLHttpRequest) { 
            xmlhttp=new XMLHttpRequest();
        } else { 
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function() {
            if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                var book_details = eval(xmlhttp.responseText);
                alert(book_details[0]["url"]);
                // To be added: extra code to populate HTML results.
                document.getElementById("book_results").innerHTML=xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","/subject_json/?id=" + $("#subjectlist").val(),true);
        xmlhttp.send();
    }); 

Thanks for any advice!

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about AJAX