2017-02-09 50 views
0

我想通過JQuery AJAX通過REST API創建SP 2013站點。我已經從'/ _api/contextinfo'調用中提取了REQUEST_DIGEST。接下來,在嘗試爲'/ _api/web/webinfos/add'調用設置X-RequestDigest頭時,瀏覽器發送帶有方法'OPTIONS'的預發送請求,該瀏覽器正在獲取HTTP 403響應代碼。根據我的理解,它期待FedAuth cookie,而根據CORS原則,瀏覽器不會發送認證信息。看來「選項」動詞需要在SP 2013上進行配置,我還沒有找到任何明確的解決方案。我的理解是否正確,在這種情況下任何人都可以提供解決方案?通過REST API通過Jquery AJAX創建SP 2013站點

+0

您是否在討論在現有網站集內創建網站集或子網站? –

+0

我正嘗試使用網站模板在現有sitecollection內創建子網站。 – user7541515

回答

0

我創建了這個代碼,它適用於我的Sharepoint 2013環境。

首先,我盡我的Ajax請求,像這樣相對URL:/_api/web/webinfos/add

的resquest導致403禁止

然後我用重試:_spPageContextInfo.webAbsoluteUrl + "/_api/web/webinfos/add"

,這一次它工作正常。

的原因是,我的網站集URL模式是:

所以,當我使用相對URL /_api /網絡/ webinfos /加其與使用:

因爲它不是當前網站集的地址Sharepoint返回一個跨站點腳本錯誤。

但是當我使用_spPageContextInfo.webAbsoluteUrl + 「/ _api /網絡/ webinfos /添加」它給我的網站集的完整URL:

這裏完整的腳本:

<script language="JavaScript" type="text/javascript"> 
function createSubsiteUsingREST(data,siteTitle,siteUrl,siteDescription) { 

    $.ajax({ 
     url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/webinfos/add", 
     type: "POST", 
     headers: { 
      "accept": "application/json;odata=verbose", 
      "content-type": "application/json;odata=verbose", 
      "X-RequestDigest": $("#__REQUESTDIGEST").val() 
     }, 
     data: JSON.stringify({ 
      'parameters': { 
       '__metadata': { 
        'type': 'SP.WebInfoCreationInformation' 
       }, 
       'Url': siteUrl, 
       'Title': siteTitle, 
       'Description': siteDescription, 
       'Language': 1033, 
       'WebTemplate': 'sts', 
       'UseUniquePermissions': true 
      } 
     }), 
     success:function(){ 
      alert('SubSite Created with success!'); 
     }, 
     error:function(){ 
      alert('oups! An error occured during the process of creating this new SubSite!'); 
     }   
    }); 
} 

$(document).ready(function() { 
    $('#btnCreateSubSiteWithREST').on('click',function() { 
     var siteTitle = $('#txtSiteTitle').val(); 
     var siteUrl = $('#txtSiteUrl').val(); 
     var siteDescription = $('#txtSiteDescription').val(); 
     createSubsiteUsingREST(siteTitle,siteUrl,siteDescription); 
    }); 
}); 
</script> 

<input type="button" id="btnCreateSubSiteWithREST" value="Create New SubSite Using REST"> 

<div><label>Title of the SubSite : </label><input type="text" id="txtSiteTitle"></div> 
<div><label>URL of the SubSite : </label><input type="text" id="txtSiteUrl"></div> 
<div><label>Description of the SubSite : </label><input type="text" id="txtSiteDescription"></div> 

希望這個幫助!

+0

感謝您的示例代碼。我使用的是absoluteUrl,我想我忘了提及該呼叫是從SharePoint服務器外部/不同域上的Web應用程序生成的,因此它是跨域調用。我們在呼叫中使用了crossDomain參數,但由於自定義報頭(X-RequestDigest),正在生成一個正被拒絕的預取請求(403)。我們需要一種配置SP來接受OPTIONS請求併發回HTTP 200的方法。 – user7541515