Please help me on this AJAX code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sreekandank
    New Member
    • Jun 2009
    • 45

    #1

    Please help me on this AJAX code

    I have written the AJAX script as follows:

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Ajax Demo...</title>
    <style type="text/css">
    .box{border:1px solid black;padding:10px}
    </style>
    <script type="text/javascript" language="JavaScript">
    var asyncRequest;
    function getContent(url)
    {
     try
     {
      asyncRequest=new XMLHttpRequest();
      asyncRequest.onreadystatechange = stateChange;             
      asyncRequest.open('GET',url,true);
      asyncRequest.send(null);
     }
     catch(exception)
     {
      alert('Request Failed');
     }
    }
    function stateChange()
    {
     if(asyncRequest.readyState==4 && asyncRequest.status==200)
     {
      document.getElementById('contentArea').innerHTML=asyncRequest.responseText;
     }
    }
    function clearContent()
    {
     document.getElementById('contentArea').innerHTML='';
    }
    </script>
    </head>
    <body>
    <h1>Moue over a player for More Information</h1>
    <img src="sachin.jpg" onmouseover='getContent("sachin.html")' onmouseout='clearContent()'/>
    <div class="box" id="contentArea">&nbsp;</div>
    </body>
    </html>
    I want to display the profile information when onmouseover event is triggered. As I am unable to run this script, kindly any one correct me on this mistake. Thanks in advance.
    Last edited by Dormilich; Feb 10 '12, 03:30 PM. Reason: please use code tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    any messages in the error console?

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      Have you tried specifying the full URL?

      Comment

      • sreekandank
        New Member
        • Jun 2009
        • 45

        #4
        exception message is displayed when the mouse cursor moves over that image.

        Comment

        • sreekandank
          New Member
          • Jun 2009
          • 45

          #5
          Yes. Already I have tried with full URL. Still its not working.. Please help me.

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            exception message is displayed when the mouse cursor moves over that image.
            and what is the exception message?

            Comment

            • sreekandank
              New Member
              • Jun 2009
              • 45

              #7
              I got the request message as "Request Failed"

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                on line 23 replace "Request failed" by exception.messa ge. what info do you get then?

                Comment

                • sreekandank
                  New Member
                  • Jun 2009
                  • 45

                  #9
                  I got the exception message says 'XMLHttpRequest 'is undefined

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    do you use IE < 8?

                    Comment

                    • sreekandank
                      New Member
                      • Jun 2009
                      • 45

                      #11
                      Yes. I am using IE6.

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        then it’s obvious. only since version 8 IE knows the XMLHttpRequest object. before that Microsoft insisted on making their own thing (using ActiveX).

                        there is a lot of implementations around, I’m taking the example from wikipedia
                        Code:
                        /*
                           Provide the XMLHttpRequest constructor for Internet Explorer 5.x-6.x:
                           Other browsers (including Internet Explorer 7.x-9.x) do not redefine
                           XMLHttpRequest if it already exists.
                         
                           This example is based on findings at:
                           http://blogs.msdn.com/xmlteam/archive/2006/10/23/using-the-right-version-of-msxml-in-internet-explorer.aspx
                        */
                        if (typeof XMLHttpRequest == "undefined")
                          XMLHttpRequest = function () {
                            try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); }
                              catch (e) {}
                            try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
                              catch (e) {}
                            try { return new ActiveXObject("Microsoft.XMLHTTP"); }
                              catch (e) {}
                            //Microsoft.XMLHTTP points to Msxml2.XMLHTTP and is redundant
                            throw new Error("This browser does not support XMLHttpRequest.");
                          };

                        Comment

                        • sreekandank
                          New Member
                          • Jun 2009
                          • 45

                          #13
                          Any one please execute the correct code and and send it back to me. Still I am struggling to execute.

                          Comment

                          • Dormilich
                            Recognized Expert Expert
                            • Aug 2008
                            • 8694

                            #14
                            does that mean you’re not able to implement the given example in your code?

                            Comment

                            • sreekandank
                              New Member
                              • Jun 2009
                              • 45

                              #15
                              Yes. Please correct my code and send it back to me.

                              Comment

                              Working...