javascript 为什么这个 jquery.get 函数不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15837205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:17:04  来源:igfitidea点击:

Why isn't this jquery.get function working?

javascriptjquery

提问by Chris Paton

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 .failfunction runs, but the textStatusand errorThrownvalues don't appear in the alert()window that pops up.

我一直在尝试创建一个小页面,它所做的只是更新源文档中的一些值。页面加载正常,但我没有从请求的源中获得结果。该.fail功能运行,但textStatuserrorThrown值没有出现在alert()弹出窗口。

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...

我对 javascript 和 jquery 很陌生。我正在尝试将其与从网络上找到的碎片一起解决以解决问题,但似乎没有任何效果。主要是,这是我认为我正在失败的回应......

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 textStatusor errorThrownresults 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.

我已经更新了我的代码:@Ian 的回答。遗憾的是,它仍然无法正常工作。我也没有得到textStatuserrorThrown结果。我已经尝试通过 VS2012 使用 Internet Explorer 进行调试,但它并没有让我走得更远。如果我将 URL 放入网页中,就可以查看 XML 文档。

回答by Ian

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

$.get不接受一个参数作为对象字面量;它接受几个:http: //api.jquery.com/jQuery.get/#jQuery-get1

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

您可能会想到$.ajax语法: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

$.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 failmethod, you can see that an error occurred and some details why.

使用该fail方法,您可以看到发生了错误以及一些详细原因。

Depending on what/where http://192.168.2.86:15890is, 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

根据内容/位置http://192.168.2.86:15890,由于同源策略,您可能无法进行 AJAX 调用 - https://developer.mozilla.org/en-US/docs/JavaScript/Same_origin_policy_for_JavaScript

I know you have some logic in your successcallback, but I'm pretty sure if you specify the dataTypeas "text", the resvariable will alwaysbe a string. So your if/elseshouldn't really do much - the elseshould never execute. Either way, if you're expecting XML, it's probably easier to just specify the dataTypeas "xml".

我知道你的success回调中有一些逻辑,但我很确定如果你指定dataType为“文本”,res变量将始终是一个字符串。所以你的if/else不应该做太多 - 不else应该执行。无论哪种方式,如果您期望使用 XML,将 指定dataType为“xml”可能更容易。