5

I've been trying to create a small page and all it does is update some values from a source document. The page loads fine, but I don't get the results from the requested source. The .fail function runs, but the textStatus and errorThrown values don't appear in the alert() window that pops up.

I'm very new to javascript and jquery. I'm trying to bash this together with pieces found from the web to figure it out but nothing seems to be working. Mainly, it's the response I think I'm falling down on...

Anyway, here's the code:

<html>
    <head>
      <title></title>
      <script type="text/javascript" src="~/Scripts/jquery-1.9.1.js"></script>

  <script type="text/javascript">

    function update() {
      $.ajax({
        type: "GET",
        url: "http://192.168.2.86:15890/linearlist.xml",
        dataType: "xml"
      }).done(function (res) {
       //alert(res);
      }).fail(function (jqXHR, textStatus, errorThrown) {
        alert("AJAX call failed: " + textStatus + ", " + errorThrown);
      });
    }

  function GetData() {
    update();
    setTimeout(function () {
      GetData();
    }, 50);
  }
});

  </script>
</head>
<body>
<script type="text/javascript">
    GetData();
</script>
  <div class="result"> result div</div>
</body>
</html>

UPDATE:

I've update my code re: @Ian's answer. It's still not working, sadly. I'm not getting the textStatus or errorThrown results either. I've tried debugging with Internet Explorer through VS2012 but it's not getting me far. If I put the URL into a webpage, I can view the XML document.

Chris Paton
  • 5,113
  • 4
  • 41
  • 52
  • Is that page being served from "192.168.2.86:15890"? If not, the browser won't let you do that unless the server is providing the appropriate "Access-Control" headers. – Pointy Apr 05 '13 at 15:01
  • 1
    Would you be more specific about the problem other than "it's not working"? Have you tried using a debugger such as firebug? – Aaron Kurtzhals Apr 05 '13 at 15:02
  • You should try changing your dataType to "xml", not sure if this will fix your problem though – Justin Bicknell Apr 05 '13 at 15:05

1 Answers1

11

$.get does not accept one parameter as an object literal; it accepts several: http://api.jquery.com/jQuery.get/#jQuery-get1

You might be thinking of the $.ajax syntax: http://api.jquery.com/jQuery.ajax/

Anyways, call it like:

$.get("http://192.168.2.86:15890//onair.status.xml", {}, function (res) {
    var xml;
    var tmp;
    if (typeof res == "string") {
        tmp = "<root>" + res + "</root>";
        xml = new ActiveXObject("Microsoft.XMLDOM");
        xml.async = false;
        xml.loadXML(res);
    } else {
        xml = res;
    }
    alert("Success!");
}, "text");

Or use $.ajax:

$.ajax({
    type: "GET",
    url: "http://192.168.2.86:15890//onair.status.xml",
    dataType: "text"
}).done(function (res) {
    // Your `success` code
}).fail(function (jqXHR, textStatus, errorThrown) {
    alert("AJAX call failed: " + textStatus + ", " + errorThrown);
});

Using the fail method, you can see that an error occurred and some details why.

Depending on what/where http://192.168.2.86:15890 is, you may not be able to make AJAX calls due to the same origin policy - https://developer.mozilla.org/en-US/docs/JavaScript/Same_origin_policy_for_JavaScript

I know you have some logic in your success callback, but I'm pretty sure if you specify the dataType as "text", the res variable will always be a string. So your if/else shouldn't really do much - the else should never execute. Either way, if you're expecting XML, it's probably easier to just specify the dataType as "xml".

Ian
  • 50,146
  • 13
  • 101
  • 111
  • Hey Ian, that's great. I used the ajax method and it's at least telling me now that I've an error, but it's not saying what. I just get `"AJAX call failed: , "` so `textStatus` and `errorThrown` aren't coming through. Also, this URL isn't on my server, so shouldn't have same-origin issues with it, right? – Chris Paton Apr 05 '13 at 15:28
  • @ChrisPaton Hmm interesting. What browser are you doing this in? You could look at the console/network and see what's happening to the AJAX request. And if it's not on your server, that doesn't matter. But if the domain is different than your page's, then that's where you'll have issues. But I'm guessing that since it's not the same server, it'll be a different domain. It's like `http://www.stackoverflow.com` trying to make an AJAX request to `http://www.google.com` - it's not allowed. – Ian Apr 05 '13 at 15:36
  • Hey Ian, I just posted a little update. Something to do with the different versions of jquery I'm using... – Chris Paton Apr 05 '13 at 15:42
  • 1
    @ChrisPaton So, in jQuery 1.9.1 (the jQuery version you're using), the `live` method is no longer available. It's replaced by `on`. So I found this SO page - http://stackoverflow.com/questions/14405178/jquery-unobtrusive-ajax-plugin-broken-when-updating-to-jquery-1-9-0 . I think it basically says to go to your `jquery.unobtrusive-ajax.js` file and convert the `live` calls to `on`. So that is an option. My question is: do you actually use that file? Myself and many people include those files or don't know what they do, and end up not using them – Ian Apr 05 '13 at 16:02
  • @ChrisPaton Same goes for the other files - do you actually use the jQuery UI library and its features? Do you use the `validate` plugin? If you say "no" to any of those, you can remove the specific file(s) from your site – Ian Apr 05 '13 at 16:03
  • I've removed them all and now stuck with just the jquery-1.9.1 version, but I'm still not getting any love. :( – Chris Paton Apr 05 '13 at 16:14
  • @ChrisPaton Good. So what happens now when you run your code without the extra scripts? Any `alert`s happening? What do you see in your browser console? – Ian Apr 05 '13 at 16:15
  • The .fail function is throwing, but still not showing any information. I take it VS2012 (my dev environment) isn't great at debugging scripts - is there a good source for debugging javascript stuff like this? – Chris Paton Apr 05 '13 at 16:27
  • @ChrisPaton Well, I'm wondering if it's a same original problem. I'm stil confused why nothing is being alerted. What computer is `192.168.2.86`? I'm also confused what the port is (15890). If you look at stackoverflow.com/questions/2770009/jquery-ajax-success-function-returns-empty#answer-2770036 , it explains some things you "can't" do with AJAX requests. – Ian Apr 05 '13 at 16:38
  • 1
    @ChrisPaton And it depends on what browser you're using. Debugging Javascript has become pretty easy. If you use a mainstream browser, they have things built in (or you can use an external one). For example, IE7 has Developer Toolbar, IE8 and IE9 (and probably IE10) has Developer Tools. Firefox has Firebug (an add on). Chrome has a different Developer Tools. They can be used to see network traffic, view the DOM, includes an interactive console, can view cookies, and can see CSS/JS (depending on the specific tool). These are usually activated by pressing F12, but can be accessed from a menu. – Ian Apr 05 '13 at 16:42
  • 1
    Thanks Ian! I found this: `SCRIPT7002: XMLHttpRequest: Network Error 0x80070005, Access is denied.` This suggests you are right and that it's an access issue. Grrr... But thanks for your help. Legend, you are! :) – Chris Paton Apr 05 '13 at 16:48