【问题标题】:cross domain issue when accessing sharepoint list from sharepoint app从共享点应用程序访问共享点列表时出现跨域问题
【发布时间】:2015-06-28 07:11:10
【问题描述】:

这是我的代码

function getListItem(url, listname, id, complete, failure) {
    // Getting our list items
    $.ajax({
        url: url + "/_api/lists/getbytitle('"+ listname +"')/items",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            // Returning the results
            complete(data);
        },
        error: function (data) {
            failure(data);
        }
    });
};

url 正确出现,同时检查 ajax 调用中的完整 url 参数,当我在新选项卡中打开它时它正在返回数据

但由于此 ajax 调用是在不同域中的共享点应用程序内进行的,因此会引发错误 - No 'Access-Control-Allow-Origin' header is present on the requested resource

我应该在我的站点中进行哪些更改以使该列表可用于跨域调用。

信息: 网站网址是http://www.vignesh.cloudappsportal.com/ 应用网址为xxxx.apps.cloudappsportal.net/CloudAppsTrial/Pages/Default.aspx

【问题讨论】:

    标签: sharepoint sharepoint-2013 sharepoint-list


    【解决方案1】:

    出于安全考虑,现代网络浏览器会阻止跨域调用。这是基于 Web 的开发中的常见问题,并且由于 SharePoint 托管应用程序的性质而特别相关。例如,当从应用程序(托管在http://apps.sharePoint.com/SPApp)访问父网站(http://consoto.sharepoint.com)中的数据时,调用将被阻止。要解决此问题,您可以使用 SP.RequestExecutor.js 脚本将消息从同一域内中继到 SharePoint。

    示例

    function getListItems(hostUrl,appWebUrl, listTitle,success,error) 
    {
        var url = appWebUrl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('" + listTitle + "')/items?@target='" + hostUrl + "'";
        var executor = new SP.RequestExecutor(appWebUrl);
        executor.executeAsync(
            {
               url: url,
               method: "GET",
               headers: { "Accept": "application/json; odata=verbose" },
               success: function (data) {
                    var data = JSON.parse(data.body);
                    success(data.d.results);
               },
               error: error
           });
    }
    

    用法

    function getParameterByName(name) {
        name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
        var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
            results = regex.exec(location.search);
        return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
    }
    
    var appWebUrl = getParameterByName('SPAppWebUrl');
    var hostUrl = getParameterByName('SPHostUrl');
    var scriptbase = hostUrl + '/_layouts/15/';
    $.getScript(scriptbase + "SP.RequestExecutor.js", function (data) {
       getListItems(hostUrl,appWebUrl, 'Tasks',function(data){
           //print list items properties
           data.forEach(function(item){
              console.log(item.Title);             
           });
    
       },
       function(error){
           //error handling goes here...   
       });
    });
    

    关于应用权限

    SharePoint 应用程序使用权限请求来指定其正常运行所需的权限。权限请求指定应用程序需要的权限以及需要权限的范围。这些权限是作为应用清单的一部分请求的。下图演示了如何为 App 授予读取权限

    关注App permissions in SharePoint 2013了解更多详情。

    【讨论】:

    • 您知道 SP.Request 执行器方法在从不同的 Web 应用程序获取数据时是否有效。 IE。 webapp1.company.com 调用 webapp2.company.com
    猜你喜欢
    • 2018-12-05
    • 2011-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 2012-12-06
    相关资源
    最近更新 更多