I am new to JavaScript and AJAX

Discussion in 'JavaScript' started by Mitchell, May 14, 2009.

  1. #1
    I am on chapter 3 of this book Bulletproof AJAX. At this web page http://bulletproofajax.com/code/
    I click Ajax Test and then click the link that follows to make an alert message pop-up and it does pop-up. I then download the 3 sample files and put them all into the same folder on my hard drive and run the same test by clicking the link. This time no alert pop-up occurs.

    1. What am I missing to make these three files work on my computer the way they work at this website?

    2. Does my computer need to be turned into a server? I have XAMPP (a Apache, MySql set up) installed and the control panel for it seems to indicate it is running. I guess that means my computer is set up like a server.

    3. Or do I need to just add something to the code, like the file name to this function? request.open("GET", "message.txt", true);
    Before it looked like this
    request.open("GET", file, true);

    Or is there something else I am missing? Thanks for any help.
     
    Mitchell, May 14, 2009 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    You have to put the files in the webserver's folder - the ajaxtest.js uses a getHTTPObject call which won't work with files. If you put the files in your webserver, and access them through your browser, they work just fine. Just tested it.
     
    PoPSiCLe, May 15, 2009 IP
  3. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for your response.

    I put the files in a folder called htdocs which is in the xampp folder. This is where I have tested other web pages. I think this is the web servers folder you speak of. I get the same result. When I click the link on the web page an error message should be displayed but it dose not display. I am using Firefox.
     
    Mitchell, May 15, 2009 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Ok? It worked just fine here, when I tested the files I downloaded on a development server I have available. I'm not familiar with XAMPP setup on Windows, so I can't help you there, sorry.

    You are sure that all three files are in the webfolder? index.html, ajaxtest.js and message.txt?
     
    PoPSiCLe, May 15, 2009 IP
  5. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yes. All three files are in the web folder. I open index.html, click on the link and nothing happens. What is supposed to happen is an error message pops up. If I put " " around file in request.open("GET", "file", true);

    function grabFile(file) {
    var request = getHTTPObject();
    if (request) {
    request.onreadystatechange = function() {
    displayResponse(request);
    };
    request.open("GET", file, true);
    request.send(null);
    }
    }

    the link will open a new page with the message.txt file in it. If I leave the " " off of the file word, an error should be displayed.

    The only thing I can think of is that I recently installed Firebug editor to Firefox. I don't know if that changed the way errors are displayed. However I tried opening index.html in Internet Explorer and get the same behavior when I click the link. Nothing.

    How is your machine set up as a server? Are you using Apache?
     
    Mitchell, May 15, 2009 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    My dev. server is a Linode (VPS online) - it's not a personal test-server. But, yeah, Firebug MIGHT make it not work - I had a similar problem earlier. Try removing Firebug?

    My server is not on apache, it's running Lighttpd - a lightweight webserver which in many ways is similar to Apache.

    Although, it shouldn't matter which server you run - the js-examples should work anyway.
     
    PoPSiCLe, May 16, 2009 IP
  7. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I thank you again for your help. I have much to learn in this area. I seem to need an investigator or detective to help me figure this out.

    I disabled Firebug and tried clicking the web page link and again nothing happens (no error dialog appears).

    I down loaded the chapter 4 example and clicked the 3 links on its web page. They do not work either. They are supposed to present new information without reloading the page.

    I have an unrelated web page on my computer that uses JavaScript but not AJAX. It works fine.

    Can I test web pages that I create on my computer without a web server installed?

    Is a web server program only needed if I am trying to access information on my computer using AJAX or a server language like PHP?

    If this is true; perhaps my web server XAMPP (cross-platform, Apache HTTP server, MySQL, PHP, Pearl) is not configured correctly.
     
    Mitchell, May 16, 2009 IP
  8. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #8
    That is correct - "normal" webpages, ie. pages that only uses HTML, CSS and JAVASCRIPT can be tested directly from the folder - just doubleclick the .html-file to show it in a browser window. As long as there is no server-side code to access/run, it should work just fine.

    Again, it might not be configured correctly, but it shouldn't matter - as long as the files get parsed, javascript and html should work just fine.
     
    PoPSiCLe, May 17, 2009 IP
  9. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks for your help.

    I think I am going to have to learn how to use Firebug to help me troubleshoot why this won't work.
     
    Mitchell, May 17, 2009 IP
  10. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I suspect my server software needs to be troubleshooted. I will look into that. Hopefully I won't have to reinstall it.
     
    Mitchell, May 18, 2009 IP