【问题标题】:Sharepoint Rest Api calls`Sharepoint Rest Api 调用`
【发布时间】:2017-02-27 09:03:09
【问题描述】:

我正在制作一个简单的网站,我必须将我的一些数据保存到共享点列表中。我只使用html css js。我需要对 Sharepoint API 进行 REST 调用以在 SP 上发布数据。我正在尝试从我的列表(在 SP 中)获取数据,如下所示:

$(document).ready(function() {
        processListItems(hostweburl, 'ListName');
    });

function processListItems(url, listname) {

        $.ajax({
            url: url + "/_api/web/lists/getbytitle('" + listname + "')",
            method: "GET",
            headers: {
                "Accept": "application/json; odata=verbose"
            },
            success: function(data) {
                console.log(data);      
            },
            error: function(data) {
                console.log(data);
            }
        });
    }

它返回这个响应:

"{"error":{"code":"-2147024891, System.UnauthorizedAccessException","message":{"lang":"en-US","value":"Access denied. You do not have permission to perform this action or access this resource."}}}"

我只引用了两个脚本:

 <script src="js/jquery-3.1.1.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js" type="text/javascript"></script>

它说我未经授权访问。虽然我对我的列表拥有管理员权限。请告诉我如何解决这个问题。我是否需要任何授权标头才能对 Rest Api 进行 ajax 调用?我是 Sharepoint 的新手。请帮忙!

【问题讨论】:

标签: javascript html rest sharepoint sharepoint-2010


【解决方案1】:

我认为 SPOnline 中默认禁用自定义脚本。您可能必须启用它。这些链接可能会有所帮助:

Access denied office 365 / SharePoint online with Global Admin account

http://vamsi-sharepointhandbook.blogspot.com/2015/07/turn-scripting-capabilities-on-or-off.html

【讨论】:

  • 我需要任何访问令牌吗?
  • 用于 Restful api 调用
  • 我可以在 ajax 调用中传递用户名和密码吗?
【解决方案2】:

当您尝试从列表中提取项目时,请指定最多项目的 URL。请参阅下面的语法 -

$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('"+listName+"')/items",
// Rest of the code
});

【讨论】:

    【解决方案3】:

    是的,您的请求需要授权标头。所以你的标题应该是这样的:

    headers: {
                 "Accept": "application/json; odata=verbose",
                 "Authorization": 'Bearer '+ **access_token**   
             }
    

    access_token:您需要获取访问令牌才能发出请求。你可以使用微软提供的adal library

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-28
      • 2018-09-15
      • 2015-09-11
      • 1970-01-01
      • 1970-01-01
      • 2021-11-10
      • 1970-01-01
      相关资源
      最近更新 更多