将变量传递给ajax回调

时间:2014-07-16 22:22:26

标签: javascript jquery ajax

在ajax回调中使用变量似乎有效,但我不明白为什么。这是一个例子。有人可以解释为什么变量(在本例中为subSiteUrl)设置在回调之外是正确的吗?

我希望变量不可靠,因为循环可能在回调发生之前进展。

function getSubWebProjInformation() {
    $.each(subWebsArray, function() {
        var subSiteUrl = this.Url;
        var targetUrl = this.Url + "/_vti_bin/lists.asmx";
        var listName = "Project Information";
        var soapEnv = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
            "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
            "   <soap:Body>" +
            "       <GetList xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\">" +
            "            <listName>" + listName + "</listName>" +
            "      </GetList>" +
            "   </soap:Body>" +
            "</soap:Envelope>";
        $.ajax({
            cache: false,
            url: targetUrl,
            type: "POST",
            dataType: "xml",
            data: soapEnv,
            contentType: "text/xml; charset=utf-8",
            beforeSend: function(xhr) {
                xhr.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/GetList");
            },
            complete: function(msg) {
                if (msg.status == 200) {
                    var projFieldsArray = [];
                    $(msg.responseXML).find("Field").each(function() {
                        var staticName = $(this).attr("StaticName");
                        var displayName = $(this).attr("DisplayName");
                        var isFromBaseType = $(this).attr("FromBaseType") === "TRUE";
                        if (!isFromBaseType && staticName && staticName != "PermMask" || staticName === "Title") {
                            var f = {};
                            f.displayName = displayName;
                            f.staticName = staticName;
                            projFieldsArray.push(f);
                        }
                    });
                    updateSubWebObjectWithProjFields(subSiteUrl, projFieldsArray);
                } else {
                    //Failure
                    var errorCode = $(msg.responseXML).find("errorcode").text();
                    var errorString = $(msg.responseXML).find("errorstring").text();
                    if (errorString.length === 0) {
                        errorString = $(msg.responseXML).find("faultstring").text();
                    }
                    errorString = errorString.replace(/(\r\n|\n|\r)/gm, "");
                    showStatusBar("Oh no! " + errorString);
                }
            },
        });
    });
}

function updateSubWebObjectWithProjFields(subSiteUrl, projFieldsArray) {
    console.log(subSiteUrl);
    $.each(projFieldsArray, function() {
        console.log(this.displayName + ": " + this.staticName);
    });
}

1 个答案:

答案 0 :(得分:0)

设置var subSiteUrl = this.Url;将允许存储this.Url内的值。在javascript变量中保存值,而不是引用。因此,当this更改时,subSiteUrl内存储的值仍然是相同的值。但是,this更改后,this.Url会有所不同。

保存this的值或this的属性可能非常有用。这是保持访问范围的最佳实践方法,我在此处写了一个规范的答案: Save access to this scope

  

我希望变量不可靠,因为循环可能在回调发生之前进展

这里发生了两件事,变量的可靠性和循环进展。首先,在进行ajax调用时,循环 正在进行中。但是,会影响变量的可靠性。该变量的范围限定为先前的匿名函数。此范围被称为“执行上下文”,并将保留变量的值,直到上下文不再使用。

总之,存储变量是一个好主意,并且在您的示例环境中使用非常安全。

相关问题