我已经为一位客户设置了Dynamics-Portal。客户希望在门户上显示来自SharePoint的图像。
我的想法是使用jQuery和Ajax来访问SharePoint-API。
由于我在web编程方面的经验相当有限,我需要帮助来理解我得到的错误。
读了之后,这是我的第一次尝试:
$.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 }
})这会产生以下错误:
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。如下所示:
url:"https://example.SharePoint.com/_api/web/.../Files?callback=?"这看起来和改变是一样的:
dataType: 'json'
to
dataType: 'jsonp'错误消失了,但出现了以下警告:
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成功-但是这个错误:
"JQuery13423412342134134_1234123412341234 was not called"再加上一个解析错误?
提前感谢并致以最良好的问候
Jek
发布于 2019-07-30 03:28:31
显然,这在这种情况下是不可能的,因为我不能更改SharePoint的CORS设置。解决方案似乎是SharePoint插件。
发布于 2019-07-26 18:49:13
尝试添加代码(support.cors):
$.support.cors = true;https://stackoverflow.com/questions/57202081
复制相似问题