可能重复:
jQuery:ajax调用成功后返回数据
因此,我正在调用 Sharepoint 服务并尝试返回一些值以供其他地方使用,并且我正在尝试声明一个窗口变量来执行此操作。看起来很简单,但是在 responseXML 函数之外未定义窗口变量。
userName = "";
var soapEnv =
"<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:tns='http://schemas.microsoft.com/sharepoint/soap/'> \
<soap:Body> \
<GetUserProfileByName xmlns='http://microsoft.com/webservices/SharePointPortalServer/UserProfileService'> \
<AccountName></AccountName> \
</GetUserProfileByName> \
</soap:Body> \
</soap:Envelope>";
$.ajax({
url: "/_vti_bin/userprofileservice.asmx",
type: "POST",
dataType: "xml",
data: soapEnv,
complete: processResult,
contentType: "text/xml; charset='utf-8'"
});
function processResult(xData, status) {
$(xData.responseXML).find("PropertyData > Name:contains('FirstName')").each(function() {
window.FirstName = $(this).parent().find("Values").text();
});
$(xData.responseXML).find("PropertyData > Name:contains('LastName')").each(function() {
window.LastName = $(this).parent().find("Values").text();
});
}
userName += window.FirstName;
userName += " " + window.LastName;
console.log(userName);