0

Experimenting with JS/Jquery and the JQuery .get function. I'm getting the pop up/alert to open but there is no text after "Data Loaded:"

What am I missing?

I have a .js file (called lab1.js) that looks like this:

var person = prompt("Please enter your name", "Harry Potter");
if (person != null) {
    document.getElementById("demo").innerHTML =
    person + "'s Favorite Links:";
}

$(document).ready(function(){
    $.get("/Users/rabdelaz/Desktop/Labs/lab1.txt", function(data){
        alert("Data Loaded: " + data);
    });
});

With the following in my lab1.txt file:

"a line of text of your choosing"

Finally here is my html:

   <!DOCTYPE html>
<html lang="en">
    <head>
        <link rel="stylesheet" type="text/css" href="lab1.css">
        <meta charset="utf-8"/>
        <title>Lab 1</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
    </head>

    <body>

        <p id="demo"></p>

        <ul>
            <li><a href="http://www.akamai.com">www.akamai.com</a></li>
            <li><a href="http://aloha.akamai.com">aloha.akamai.com</a></li>
            <li><a href="http://agora.akamai.com">agora.akamai.com</a></li>
            <li><a href="http://ask.akamai.com">ask.akamai.com</a></li>
            <li><a href="http://luau.akamai.com">luau.akamai.com</a></li>
            <li><a href="http://mydrive.akamai.com">mydrive.akamai.com</a></li>
            <li><a href="http://act.akamai.com">act.akamai.com</a></li>
            <li><a href="http://tools.gss.akamai.com/portal">GSS Tools</a></li>
        </ul>

        <script src="lab1.js"></script>

    </body>
</html>    
Ramy
  • 20,541
  • 41
  • 103
  • 153
  • 1
    You running this off the file protocol? – epascarello Sep 22 '14 at 17:17
  • 1
    Is the file path to lab1.txt correct? If it's in the same directory as lab1.js, then just write it as `lab1.txt` instead of `/Users/.../lab1.txt`. – jrad Sep 22 '14 at 17:18
  • running the file from local server?? – Raja Sekar Sep 22 '14 at 17:19
  • @radcliffejh Wouldn't solve the underlying issue, though. – Dave Newton Sep 22 '14 at 17:22
  • Wow, jQuery 1.3.2?!! Party likes it's 1999! You should use a current version – Macsupport Sep 22 '14 at 17:28
  • 1
    If you pop up the developers tools in your browser (ctrl+shift+J in some browsers), there may be a network tab where you can see what happens to that get request. Also, consider adding a .fail() to your get request, see http://stackoverflow.com/questions/5047906/how-to-detect-jquery-get-failure-seeking-simple-code-example – Paul Sep 22 '14 at 17:32
  • @epascarello: file protocol? I don't believe so. I don't know what that is/what you mean. – Ramy Sep 22 '14 at 18:55
  • Are you clicking a file on your computer [e.g. file://C:/Users/foo...] or is it being run off a server [e.g. `http://localhost/...`]? – epascarello Sep 22 '14 at 18:56
  • @radcliffejh: Path is correct. I've also tried without the fully qualified name since the file is in the same directory as lab1.js. – Ramy Sep 22 '14 at 19:08
  • @MacSupport: I could use a newer version but it should work with this version according to my lab. Will try to rule that out. – Ramy Sep 22 '14 at 19:09
  • @Paul: thanks, will look at that more closely soon. – Ramy Sep 22 '14 at 19:09
  • @epascarello: I'm opening the file locally in my webrowser. No server involved here. Everything is local. So yes in the address bar it's file://Users.... – Ramy Sep 22 '14 at 19:10
  • 1
    http://www.chrome-allow-file-access-from-file.com/ – epascarello Sep 22 '14 at 19:24
  • for the mac: http://blog.derraab.com/2013/05/30/start-google-chrome-with-local-file-access-on-mac-os-x/ – Ramy Sep 22 '14 at 19:50

0 Answers0