0

Issue: index.html works fine. I can click on button, but nothing in infodiv changes. So the Ajax code is broken!

Files location: I run the files on my desktop visual studio code. All images are inside img folder. img folder, all the html files, ajax.js are inside Ajax folder

How I run the program: I click index.html and choose "open in default browser" like how i run all other html files. Tried to search for if there is some commands needed in terminal to run ajax, since like no need - run regularly

index.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta name="viewport" content="intial-scale=1,width=device-width">
        <script src="https://code.jquery.com/jquery-1.11.0.min.js" type="text/javascript"></script>
        <script src="ajax.js" type="text/javascript"></script>
        <title>Ajax</title>
    </head>
    <body>
        <h2>Ajax Test</h2>
        <div id="infodiv">
            Information goes here
        </div>
        <div>
            <form>
                <select name="staff" onchange="cs50Info(this.value);">
                    <option value="">Select A Dog:</option>
                    <option value="golden">Golden Retriver</option>
                    <option value="bernese">Bernese Mountain Dog</option>
                    <option value="samoyed">Samoyed</option>
                    <option value="husky">Husky</option>
                </select>
            </form>
        </div>

    </body>
</html>

ajax.js

function cs50Info(name)
{
    //deal with the situation where nothing is chosen
    if (name === "")
        return;

    // create a new AJAX object
    var ajax= new XMLHttpRequest();

    // change content inside the infodiv
    ajax.onreadystatechange = function() {
        if (ajax.readyState === 4 && ajax.status === 200) {
            $('infodiv').html(ajax.responseText);
        }
    }
    //open the requested file and going to update just the infodiv
    ajax.open('GET', name + '.html', true);
    ajax.send();
}

golden.html example: (all other ones are similar)

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta name="viewport" content="intial-scale=1,width=device-width">
        <title>Dogs</title>
    </head>
    <body>

   <p>Golden Retriver</p> 
   <img src="img/golden.jpeg">
   <p>Golden Fluffy Retriever</p>
   <p>So Adorable!</p>
</body>
</html>
8
  • which course is this from? which problem?
    – UpAndAdam
    Commented Jan 24 at 15:32
  • @UpAndAdam it is week9 Flask course. I try to copy the code from the course shorts video “Ajax” since there is no source code can’t find
    – TrytoLearn
    Commented Jan 24 at 16:40
  • One difference with the video: golden.html is not a "snippet" (at 11:15). Use the browser console and debugger to get eyes on what is happening. Perhaps check out [AJAX Introduction}(w3schools.com/xml/ajax_intro.asp) at W3 schools. Commented Jan 25 at 12:18
  • there are multiple CS50 courses which cover flask. That is why I am asking. is this CS50 Python, or CS50 Webprogramming etc. week9 flask is not unique. and its not flask you have no python/flask it looks like raw html and javascript (with javascript ajax)
    – UpAndAdam
    Commented Jan 25 at 17:51
  • @UpAndAdam cs50.harvard.edu/x/2024/shorts/ajax
    – TrytoLearn
    Commented Jan 26 at 16:22

1 Answer 1

1

There are typos on lines 13 and 15 of ajax.js. 13 should have a # before invdiv, 15 should be ; terminated. It would also be wise to use a snippet for the code inserts as per the video instead of a "full" html otherwise it will create invalid html which may or may not cause problems downstream.

Nice work transcribing the code from the video!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .