SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard mPeror's Avatar
    Join Date
    Mar 2005
    Location
    Saudi Arabia
    Posts
    1,725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ajax not working (prototype)

    I'm using Prototype's AJAX to get the "alert" the response of a simple PHP script (a script that echoes a single line), but it doesn't seem to work. Here's the code:

    Code:
      
      function init()
      {
        var url = "http://localhost/script.php";
        var myAjax = new Ajax.Request(
                    url,
                    {
                      onSuccess: showResponse
                    });
      }
      
      function showResponse(response)
      {
        alert(response.responseText);
      }
    
      window.onload = init;
    The code has no errors, but still doesn't show the response even if i add an onFailure event handler. What could be wrong?

  2. #2
    Caveat surfer Buddy Bradley's Avatar
    Join Date
    May 2003
    Location
    Cambridge, UK
    Posts
    2,366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you need brackets after the first showResponse?
    Code:
    onSuccess: showResponse()

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To debug I would comment out the alert you have and try alerting just the response code. Make sure you are getting a '200'. From there you should be able to hone in on the problem.

  4. #4
    SitePoint Wizard mPeror's Avatar
    Join Date
    Mar 2005
    Location
    Saudi Arabia
    Posts
    1,725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, Buddy, i'm afraid not.

    snarky, i don't quite understand what you mean, but i've added an 'onException' option and it did call the alert, but i don't know what's the nature of the error.

    Does the files (HTML and JS) have to be on a web server in order to work?

  5. #5
    SitePoint Member
    Join Date
    Aug 2003
    Location
    California
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you have firefox, I would install the firebug extension and then choose 'Show XMLHttpRequests' from the option menu. This will log all outgoing requests and responses.

    otherwise, your prototype code looks fine....

  6. #6
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i had a similar problem but then i put the response function above what would be my init function
    then it worked, weird I know


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
  •