Trouble running Ajax function

preview
jquery
javascript
cs50

#1

None of my Ajax code will run when I bring up a web browser using the Preview button, which I added back to my IDE along with the Run button (thanks, again, for that). Ditto for the same program provided by the professor. Is there another way to open an html file, containing JS/JQUERY/AJAX, in a web-browsing window without hitting the preview button? If so, how? Could that be the problem?

I’m at a loss, as I have already checked my Chrome settings and pop-up blockers. I have run similar JS/JQ code without incident, so the problem is specific to Ajax. Representative code is below. Thanks in advance!

<!DOCTYPE html>

<html>
<head>
    <script src="https://code.jquery.com/jquery-latest.min.js"></script>
    <script>
        function quote()
        {
            var url = '/quote?symbol=' + $('#symbol').val();
            $.getJSON(url, function(data) {
                alert(data.price);
            });
        }
    </script>
    <title>ajax0</title>
</head>
<body>
    <form onsubmit="quote(); return false;">
        <input id="symbol" placeholder="Symbol" type="text"/>
        <input type="submit" value="Get Quote"/>
    </form>
</body>
</html>

#2

In order to open your preview without hitting the preview button, you can click the Share button in the top right. From there, you’ll see three links. One is your application URL. This is what you want :ok_hand: You can copy that and open it in another tab.

Once you’ve opened that, open the browser console and that will show you any errors that are occurring. From there, you’ll likely get something that is Google-able or searchable on Stack Overflow.

Side note: The way you refer to Ajax sounds like a specific language or something :slight_smile: Ajax is simply a concept. There are no ajax files. Actually Asynchronous JavaScript and XML is not always applicable because many people choose to use JSON or another format over XML. You probably already knew this but I just wanted to make sure there is no confusion.


#3

Brady,

There is sooo much confusion, and this code soooo won’t run. And you’d think code from a Harvard grad student would be bullet proof, right?

Ok, I did what you said, made the file public, pasted the following link (after opening the above file) into my browser, https://ide50-rcd1693.cs50.io, then got the following error message:

No Application Seems to be Running Here:
Check that the workspace name (ide50) and username (rcd1693) are typed correctly.
Check that the server is successfully running on Cloud9:
If the server hit an error, the output window will have a message telling you what it is
If you’re in the middle of debugging code, your server might be paused right now
The server might be running on a different port; make sure it’s on port $PORT with $IP as the IP address

Note, there is no Flask back end that should be running with this file.

What should I do next? You are the only person who has responded to this out of the three forums I’ve consulted, so much appreciated!


#4

My mistake, try the third link, the preview one. That should show you your static file (meaning you don’t need a server to be running to show it).


#5

One step closer insofar as that brought me to an AutoIndex like directory of my entire workspace and allowed me to select the file via hyperlink. It doesn’t do what I want it to, but it renders the page. Inspection reveals that the url isn’t magically doing what I want it to or what it did for the professor. It’s appending text to the end of my ide’s url address, not submitting a query to yahoo finance. This, I fear, implies I will be researching 1 to 2 lines of code for quite a while. At least now, though, I can rule out how I was bringing the file up in my browser as the source of my problems. Thanx again for the assist, Brady!


#6

Excellent! Sounds like you’ve got some absolute vs relative link research ahead of you :wink: