最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why isn't this jquery.get function working? - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Apr 5, 2013 at 16:26 Chris Paton asked Apr 5, 2013 at 15:00 Chris PatonChris Paton 5,2334 gold badges42 silver badges53 bronze badges 3
  • 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 Commented Apr 5, 2013 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 Commented Apr 5, 2013 at 15:02
  • You should try changing your dataType to "xml", not sure if this will fix your problem though – Justin Bicknell Commented Apr 5, 2013 at 15:05
Add a ment  | 

1 Answer 1

Reset to default 11

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

You might be thinking of the $.ajax syntax: http://api.jquery./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/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".

发布评论

评论列表(0)

  1. 暂无评论