首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何解决通过AJAX-request访问SharePoint-API时的CORS问题?

如何解决通过AJAX-request访问SharePoint-API时的CORS问题?
EN

Stack Overflow用户
提问于 2019-07-25 20:33:59
回答 2查看 579关注 0票数 0

我已经为一位客户设置了Dynamics-Portal。客户希望在门户上显示来自SharePoint的图像。

我的想法是使用jQuery和Ajax来访问SharePoint-API。

由于我在web编程方面的经验相当有限,我需要帮助来理解我得到的错误。

读了之后,这是我的第一次尝试:

代码语言:javascript
复制
$.ajax({
          url: "https://example.SharePoint.com/_api/web/GetFolderByServerRelativeUrl('/ImageFolder')/Files",
          type: 'GET',
          Accept: "application/json;odata=verbose",
          crossDomain: true,
          contentType: 'application/json;odata=verbose',
          dataType: 'json',
          xhrFields: { withCredentials: true }
        })

这会产生以下错误:

代码语言:javascript
复制
Access to XMLHttpRequest at 'https://example.SharePoint.com/_api/web/GetFolderByServerRelativeUrl('/ImageFolder')/Files'
    from origin 'https://myDynamicsPortal.com' has been blocked by CORS policy:
    Response to preflight request doesn't pass access control check:
    The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.
    The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

现在我通过添加"?callback=?“来更改上面的Ajax。添加到URL。如下所示:

代码语言:javascript
复制
url:"https://example.SharePoint.com/_api/web/.../Files?callback=?"

这看起来和改变是一样的:

代码语言:javascript
复制
dataType: 'json'
to
dataType: 'jsonp'

错误消失了,但出现了以下警告:

代码语言:javascript
复制
Cross-Origin Read Blocking (CORB) blocked cross-origin response
https://example.SharePoint.com/_api/web/GetFolderByServerRelativeUrl('/ImageFolder')/Files?callback=jQuery13423412342134134_1234123412341234&_=12341341234 
with MIME type application/atom+xml.

See https://www.chromestatus.com/feature/5629709824032768 for more details.

状态码是200,但出现错误:"JQuery13423412342134134_1234123412341234 was not called"

我从这些错误、警告和我的研究中理解/解释的是:

一旦需要凭据,服务器就不能再对允许的源使用通配符。由于我无法更改SharePoints源策略,因此我寻找了其他方法来解决此问题。

服务器似乎不理解我期望回调,因为我没有配置"jsonp“或"?callback=?”。这样做之后,错误就消失了,但我的门户似乎不会接受由于CORB (跨域读取阻止)而导致的响应。

为什么SharePoint突然接受了我的请求?(真的是这样吗?)

为什么我得到一个http 200成功-但是这个错误:

代码语言:javascript
复制
"JQuery13423412342134134_1234123412341234 was not called"

再加上一个解析错误?

提前感谢并致以最良好的问候

Jek

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-07-30 03:28:31

显然,这在这种情况下是不可能的,因为我不能更改SharePoint的CORS设置。解决方案似乎是SharePoint插件。

票数 0
EN

Stack Overflow用户

发布于 2019-07-26 18:49:13

尝试添加代码(support.cors):

代码语言:javascript
复制
$.support.cors = true;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57202081

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档