2016-07-24 49 views
0

如何使Sharepoint對象模型能夠從外部網絡服務獲取請求?我試圖用ajax請求:Sharepoint如何獲取外部網絡服務的請求

$.get("http:/servername/webservice_name", function(data, status) { 
alert("Data: " + data[0] + "\nStatus: " + status); 

});

但是我有一個錯誤:訪問被拒絕。 我的web服務以JSON格式進行響應。 我已閱讀了很多文章,但沒有任何決定。我讀到,ajax禁止在SharePoint中使用外部Web服務。我怎樣才能讓它沒有Ajax?

+0

Sharepoint版本?該服務是否使用相同的域名? –

+0

Sharepoint 2013在線。不,網絡服務有其他域 – Dim

+0

訪問被拒絕。意味着用戶在外部網站沒有權限 – bresleveloper

回答

0

在SharePoint聯機中,它僅適用於https Web服務。來自https網站的http不允許使用。 最終代碼,與https協同工作:

$(document).ready(function(){ 
$.ajax({ 
    url: "https://services.odata.org/Northwind/Northwind.svc/Customers", 
    type: "GET", 
    headers: { "ACCEPT": "application/json;odata=verbose" }, 
    async: false, 
    success: function (data) { 
     if(data.d.results.length>0){ 
      alert("Results Count:"+data.d.results.length); 
     }else{ 
      alert("no data"); 
     } 
    }, 
    error: function() { 
     //alert("Failed to get details");     
    } 
}); 

});

0

如果您認爲使用AJAX或特定庫阻止您訪問Web服務,則可以嘗試直接使用本機JavaScript XMLHttpRequest調用Web服務。

例如:

var verb = "GET"; 
var url = "http://servername/webservice_name"; 
var xhr = new XMLHttpRequest(); 
xhr.open(verb, url, true); 
xhr.setRequestHeader("Content-Type","application/json"); 
xhr.onreadystatechange = function(){ 
    if(xhr.readyState == 4){ 
     myCallbackFunction(xhr.status, xhr.responseText); 
    } 
}; 
xhr.send(data); 

function myCallbackFunction(status, text){ 
    // do something with the results based on the status 
} 

你也應該確認您的Internet Explorer設置是一樣的SharePoint,因爲它們是在HTML頁面,在這裏你能得到Web服務的工作。具體來說,您需要檢查瀏覽器模式和安全設置。

在嘗試排除網絡或代碼故障時,確認設置完全相同時問題仍然存在。