2013-10-01 24 views
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; 

回答

1

我認爲你正在做的太多了。此行:

$ .support.cors = true;

應該是你在奧德需要啓用跨域的唯一的事情。這告訴jQuery啓用cors,然後處理剩下的事情。

一旦這些設定,可以可以撥打SPServices正常的方式和確保你設置了「WEBURL」選項指向域2。

注意的是,雖然這會在「現代」瀏覽器,IE瀏覽器(我認爲),會提示用戶授予權限。

相關問題