繁体   English   中英

尝试存储ajax对全局变量的响应,但未在ajax代码外部分配该值

[英]Trying to store the response of ajax to a global variable but that value is not assigned outside the ajax code

还有一件事情是,如果我在ajax代码的末尾使用alert(),则代码可以正常工作。 在这里,无论ajax响应如何,都必须附加到现有表中。

for (var i = 1; i < rows.length; i++) {
                  var cells = splitCSVtoCells(rows[i], ",");

                   var obj = {};
                   for (var j = 0; j < cells.length; j++) {
                        obj[first_Row_Cells[j]] = cells[j];
                   }
                   jsonArray.push(obj);
              }
              console.log(jsonArray);for (var i = 0; i < jsonArray.length-1; i++) {                                         

                  html += "<tr id=\"rowitem" + i + "\"><td  id=\"rownum"+i+ "\" style=\"display:none;\">" + i + "</td><td> " + jsonArray[i].Clientgroup + " </td>";
                  html += "<td>" + jsonArray[i].hostgroup + "</td>";
                  html += "<td>" + jsonArray[i].server + "</td>";
                  html += "<td>" + jsonArray[i].Group + "</td>";
                  html += "<td>" + jsonArray[i].user + "</td>";
                  html += "<td>" + jsonArray[i].ticket + "</td>";
                  html += "<td>" + jsonArray[i].requesttype + "</td>";

                 $.ajax({
                      url : "BulkValidateServlet",
                      type : "POST",
                      data : {clientgroup: jsonArray[i].Clientgroup, hostgroup: jsonArray[i].hostgroup, server: jsonArray[i].server 
                      },success : function(response){
                          //alert("i in of ajax:"+i);
                            status = response;                             
                               if (status) {
                                   //alert("outside if: " + status);
                                   //$('<img src=\'images/check_mark.png\' height=\'20\' width=\'20\'/></td>').appendTo(tr);
                                    html += "<td id=\"result"+ i + "\"><img src='images/check_mark.png' height='20' width='20'/></td>";
                                } else {
                                   //alert("outside else: " + status);
                                   //$('<img src=\'images/cross_mark.png\' height=\'20\' width=\'20\'/></td>').appendTo(tr);
                                   html += "<td id=\"result"+ i + "\"><img src='images/cross_mark.png' height='20' width='20'/></td>";
                                }
                                console.log("Data: " + status);
                           //   alert("Data: " + status );
                        //return status;   
                        }
                      });

                   console.log("outside: " + status);

                   alert();


                       }


   document.getElementById('tbodyLeads').innerHTML = html;

                   }

您可以尝试添加

async: false

到您的ajax属性。

请参考此链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM