jQuery:if 语句中的 $.ajax 总是被调用(不应该!)

发布于 2024-12-08 12:38:28 字数 1083 浏览 0 评论 0原文

大家早上好


编辑:对书面错误表示歉意!我的想法是展示一个最小的例子,我在这里特别提出了它。但主要思想应该是可以理解的。

我还尝试将“外部”ajax 结果存储到数组中。这工作正常,但问题是一样的。我猜问题是不同 ajax 调用的异步处理。因此,我有时会得到“两次”结果,而不是两个不同的结果,而且我得到结果为时已晚。

我想我必须使用类似这样的队列矿石。

我的 jQuery 脚本有问题。最小的看起来像这样:

// - First call "outer" .ajax
$.ajax({
  type: "GET", url: "FILE.xml", dataType: "xml",
  success: function( outerxml ) {

    $( outerxml ).find("content").each( function() {

      var x = $(this).attr('id'); // - Get id attribute
      if ( x == 'doit' )
      {
        $.ajax({
          type: "GET", url: "FILE.xml", dataType: "xml",
          success: function( innerxml ) {
             alert("inner ajax");
             ...
          } // - End of inner success
        }); // - End of inner .ajax
      }
      alert("end of if statement");

    }); // - End .find("content")

  } // - End outer success
}); // - End outer .ajax

请注意,if 条件为“false”(x!=“doit”)。问题是脚本运行到 if 语句的末尾(我得到“if 语句结束”警报),然后脚本调用 ajax (但不应该)。在“if 语句结束”警报之后,我收到“内部 ajax”警报。

我根本找不到解决方案或问题。有一些提示吗? :) 请。

多谢!

Good Morning everybody


EDIT: Sorry bout the written mistakes! The idea was to show a minimal example and i tipped it specially in here. But the main idea should be understandable.

I also tried to store the "outer" ajax result into an array. That works fine but the problem is the same. I guess that the problem is the asynchronouse handling of the different ajax calls. Because of that i get the result sometimes "twice" instead of two different results and i get the result too late.

I guess that i have to use the queue ore something like this.

I have a problem with my jQuery script. The minimal looks something like this:

// - First call "outer" .ajax
$.ajax({
  type: "GET", url: "FILE.xml", dataType: "xml",
  success: function( outerxml ) {

    $( outerxml ).find("content").each( function() {

      var x = $(this).attr('id'); // - Get id attribute
      if ( x == 'doit' )
      {
        $.ajax({
          type: "GET", url: "FILE.xml", dataType: "xml",
          success: function( innerxml ) {
             alert("inner ajax");
             ...
          } // - End of inner success
        }); // - End of inner .ajax
      }
      alert("end of if statement");

    }); // - End .find("content")

  } // - End outer success
}); // - End outer .ajax

Please note that the if-condition is "false" (x != "doit"). The problem is that the script runs to the end of the if-statement (i get the "end of if statement"-alert) and AFTERWARDS the script calls the ajax (but it should not). I get the "inner ajax"-alert after the "end of if statement" alert.

I cannot find a solution or the problem at all. Are there some hints? :) Please.

Thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁我热情 2024-12-15 12:38:28

一行有一个 if 语句,下一行有一个左大括号。我认为你在这里遇到了 JavaScript 最邪恶的“功能”:自动分号插入

基本上,你有

if ( x == 'doit' )
{
    // code
}

但它被解析为

if (x == 'doit'); // <-- note semicolon inserted here

// Now this block is logically disconnected from the if statement
{
    // code
}

这个故事的寓意是要非常小心在 JavaScript 代码中引入换行符的位置,因为该语言希望尽可能将行尾视为语句结束。

You have an if statement on one line, and an opening brace on the next line. I think you're running into JavaScript's most evil "feature" here: automatic semicolon insertion.

Basically, you have

if ( x == 'doit' )
{
    // code
}

But it is being parsed as

if (x == 'doit'); // <-- note semicolon inserted here

// Now this block is logically disconnected from the if statement
{
    // code
}

The moral of the story is to be very careful about where you introduce line breaks in JavaScript code, because the language wants to treat end-of-line as end-of-statement whenever it can.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文