Sharepoint 2013中的外部Ajax调用:访问被拒绝

时间:2013-08-01 00:01:06

标签: ajax sharepoint sharepoint-2013 access-denied

所以,我正在尝试制作一个非常简单的sharepoint应用程序。我只是希望在sharepoint页面标题中加载已构建和托管的Web应用程序。

因此,目标只是从备用页面加载Html并将其放在Sharepoint应用程序的Default.aspx上的div中。这与我们将另一个外部项目引入非共享点Web应用程序完全没有困难的方式相同。

所以,我做了以下ajax调用:

$.ajax(
        {
            type: "GET",
            url: "http://PageIWantToLoad/default.aspx",
            dataType: "html",
            success: function (result) {
                $("#pageContainer").html(result);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert("oops");
            }

    });

但是,我收到Access is Denied错误。

我已经检查了一些类似的StackOverflow和其他在线帮助请求,但所有这些请求似乎都在处理更复杂的系统/情况。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

考虑一下这篇文章:http://msdn.microsoft.com/en-us/library/jj164022.aspx。 如果要从SharePoint发出javascript请求,则需要使用Sharepoint跨域库。 我的示例是从SharePoint本身请求SharePoint 2013 REST服务。 我使用了Script on Demand库和mQuery(SharePoint内置的jQuery模拟)。 和SP.RequestExecutor - 这是跨域SharePoint库。

SP.SOD.executeFunc('mQuery.js', 'm$', function() {
m$.ready(function() { 
    SP.SOD.registerSod('sp.requestexecutor.js',
        '/_layouts/15/sp.requestexecutor.js');
    SP.SOD.executeFunc('sp.requestexecutor.js', 
        'SP.RequestExecutor', 
    function() {
        var targetSiteUrl = "http://mySiteUrl";
    var targetUrl = "http://mySiteUrl/_api/web/lists/getByTitle('myListTitle')/items(1)";
var re = new SP.RequestExecutor(targetSiteUrl);
re.executeAsync({
    url: targetUrl,
    headers: { "Accept": "application/json; odata=verbose" },
    method: 'GET',
    success:function(response) {
        var jsonObject = JSON.parse(response.body);
            }
        });
    });
})});

关键是RequestExecutor。 如果要从外部资源请求SharePoint,则需要accessToken。 希望这会有所帮助。