Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml “中关于jquery$.ajax的问题”;至于;环_Xml_Jquery - Fatal编程技术网

Xml “中关于jquery$.ajax的问题”;至于;环

Xml “中关于jquery$.ajax的问题”;至于;环,xml,jquery,Xml,Jquery,我使用$.ajax读取“for”循环中的xml信息 这是我的xml文件: <application> <app id="id-1"> <html_code> <div id="id-1" class="portlet"> <div class="portlet-header"Links</div> <div class="portle

我使用$.ajax读取“for”循环中的xml信息

这是我的xml文件:

<application>
    <app id="id-1">
  <html_code>
          <div id="id-1" class="portlet">
               <div class="portlet-header"Links</div>
               <div class="portlet-content">id-1</div>
          </div>
  </html_code>
   </app>
    <app id="id-2">
           <html_code>
                <div id="id-2" class="portlet">
                     <div class="portlet-header"Links</div>
                     <div class="portlet-content">id-2</div>
                </div>
           </html_code>
   </app>
<application>
那么容器可能是正确的

那么为什么会发生这种情况呢?我怎样才能解决这个问题


谢谢:)

我认为这是由于AJAX请求的不连续性造成的。尝试将
async
属性设置为
false

同步请求可能会暂时停止 锁定浏览器,禁用任何 请求处于活动状态时的操作


我相信有两件事正在发生。首先,$temp_id变量被提升到函数的顶部,因此它相当于这样做:

$.ajax({
   ……
  success:function(){
       var $temp_id;
       for (var i = 0; i < $children.length; i++) {                        
          $temp_id = $children.eq(i).attr("id");
$.ajax({
……
成功:函数(){
var$temp_id;
对于(var i=0;i<$children.length;i++){
$temp_id=$children.eq(i).attr(“id”);
第二,尽管$temp_id在第一个循环中等于“id-1”,但在第二个循环中它会被更改为“id-2”,因此在第一次调用成功回调时,它已经被更改为“id-2”

这将解决您的问题:

更新2010-12-03:修复了一个bug

$.ajax({
   ……
    success:function(){
         for (var i = 0; i < $children.length; i++) {                        
            var $temp_id = $children.eq(i).attr("id");  //"$children" have defined in above 

            $.ajax({
                type: "get",
                url: "Database/App_all.xml",
                dataType: "html",
                success: function($tid) {
                    return function (xml) {
                        var $temp_code = $(xml).find("app[id='" + $tid + "']").find("html_code").html();
                        $($temp_code).appendTo($("#contain")).hide().show('slow');
                    }
                }($temp_id),
                error: function () { }
            });

        }    
    }
});
$.ajax({
……
成功:函数(){
对于(var i=0;i<$children.length;i++){
var$temp_id=$children.eq(i).attr(“id”);/“$children”已在上文中定义
$.ajax({
键入:“获取”,
url:“Database/App_all.xml”,
数据类型:“html”,
成功:功能($tid){
返回函数(xml){
var$temp_code=$(xml).find(“app[id=”+$tid+“]]).find(“html_code”).html();
$($temp_code).appendTo($(“#包含”).hide().show('slow');
}
}($temp_id),
错误:函数(){}
});
}    
}
});
我正在做的是将$temp_id传递给返回另一个函数的函数,该函数将在成功回调中调用。现在在成功回调中使用$tid是安全的,因为当$temp_id在第二个循环中更改时,它不会受到影响

更新:对hh54188以下评论的回应

使用“警报”可以中断执行过程,并允许意外调用ajax回调。IE和Firefox就是这种情况。另一方面,Chrome的行为与此不同。要测试这一点,可以运行以下代码:

for (var i = 0; i < 2; i++) {

    //if (i == 1) alert('stopping execution');

    console.log('loop: ' + i);
    $.ajax({
        url: "Database/App_all.xml",
        dataType: "html",
        success: function () {
            console.log('callback');
        }
    });
}
for(变量i=0;i<2;i++){
//如果(i==1)警报(“停止执行”);
log('loop:'+i);
$.ajax({
url:“Database/App_all.xml”,
数据类型:“html”,
成功:函数(){
log('callback');
}
});
}
您将看到控制台中的输出是:
循环:0
循环:1
回调
回调

现在用警报取消对该行的注释。在Firefox和IE中,您将看到控制台中的输出现在是:
循环:0
回调
循环:1
回调


在显示警报框时调用第一个回调。警报实质上改变了代码的行为。在使用JavaScript开发时,使用“警报”可能会令人沮丧,因为它会导致代码执行不可预测。因此,我不建议使用“警报”相反,使用console.log().console.log()更容易预测适用于所有现代浏览器和IE 8+。对于较旧的IE,您需要将文本输出到DOM。

我建议不要将async设置为false,因为这样会在从服务器获取内容时阻止执行/UI线程。为什么要请求“App\u all.xml”
$children.length
times…?将for循环移动到你的应用程序的成功功能\u all-request中。很抱歉,它不起作用,firebug警报没有错误……但是你教会了我很多,谢谢。很抱歉。我修复了我的错误,这次也测试了它。:-)请再试一次。它现在应该可以工作了。谢谢,我明白了,它确实工作得很好,但我仍然有一个问题,为什么要在(var I=0;I<$children.length;I++)之后插入“alert();”,也可以吗?这是因为alert会破坏执行过程,并允许ajax回调过早运行。IE和Firefox就是这种情况,但Chrome不是。我无法正确地将代码粘贴到该注释中,因此我将在上面的“回答”中详细说明。非常感谢mych!感谢您的耐心!
$.ajax({
   ……
    success:function(){
         for (var i = 0; i < $children.length; i++) {                        
            var $temp_id = $children.eq(i).attr("id");  //"$children" have defined in above 

            $.ajax({
                type: "get",
                url: "Database/App_all.xml",
                dataType: "html",
                success: function($tid) {
                    return function (xml) {
                        var $temp_code = $(xml).find("app[id='" + $tid + "']").find("html_code").html();
                        $($temp_code).appendTo($("#contain")).hide().show('slow');
                    }
                }($temp_id),
                error: function () { }
            });

        }    
    }
});
for (var i = 0; i < 2; i++) {

    //if (i == 1) alert('stopping execution');

    console.log('loop: ' + i);
    $.ajax({
        url: "Database/App_all.xml",
        dataType: "html",
        success: function () {
            console.log('callback');
        }
    });
}