1
這是我在Stackoverflow的第一篇文章。希望你們能幫我解決我的問題。我在domain1.sharepoint.com上有一個HTML頁面,我正嘗試使用JQuery + SPServices從domain2.sharepoint.com上的SharePoint獲取所有列表項。使用SPServices從另一個域獲取Sharepoint列表中的項目
據我所知,由於同源策略,我需要使用XMLHttpRequest(CORS),以獲得我所需要的數據。不幸的是,我不確定如何將XMLHttpRequest放入SPServices。我試過了,出現'未定義'的錯誤。我對JavaScript很感興趣,非常感謝所有的幫助。
這是我到目前爲止已經完成:
$.support.cors = true;
function createCORSRequest(method, refURL) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
xhr.open(method, refURL, true);
} else if (typeof XDomainRequest != "undefined") {
xhr = new XDomainRequest();
xhr.open(method, refURL);
} else {
xhr = null;
}
return xhr;
}
function makeCorsRequest() {
var refURL = 'http://domain1.sharepoint.com';
var spURL = refURL;
var xhr = createCORSRequest('GET', spURL);
if (!xhr) {
alert('CORS not supported');
return;
}
xhr.onload = function() {
$().SPServices({
operation: "GetListItems",
webURL: spURL,
listName: "Message Log",
viewName: "{70DFBF9A-5266-4D69-AD01-E848301B51BB}",
async: false,
CAMLViewFields: "<ViewFields><FieldRef Name='Status' /></ViewFields>",
completefunc: function (xData, status) {
$("#countlist").html($(xData.responseXML).find("z\\:row").length);
}
});
};
xhr.onerror = function(e, jqxhr) {
alert('Woops, there was an error making the request. ' + jqxhr);
};
xhr.send();
}
onload = makeCorsRequest;