需要有关使用Ajax Call的sharepoint 2013 Rest API的帮助

时间:2015-07-20 10:22:03

标签: sharepoint sharepoint-2010 sharepoint-2013

需要有关使用Ajax Call的sharepoint 2013 Rest API的帮助。

我正在尝试将发布网站中的列表项读取到团队网站。这两个网站都在不同的网站集中。

以下代码在Internet Explorer中是有效的,而不是在Google Chrome中。

$(document).ready(function() {
    $.support.cors = true;
    $.ajax({
        url:"http://icon.heart.com/WorkTools/Organization/Claim/_api/web/lists/getByTitle('Claims Links')/items?$top=200",
        type:"GET",
        headers:{"accept":"application/json;odata=verbose"},
        dataType: "json",
        success: function(data){ alert("pass")}
        error: function(Data){ alert ("Fail");}
    });  
});

响应的Http状态代码为401$.ajax请求中的错误是

  

无法加载资源:服务器响应状态为401(未授权)

错误2:

  

XML HttpRequest无法加载否请求的资源上存在“Access-control-Allow-Origin”标头。因此,Oringin'url'不允许访问。

我无法访问服务器。我只需要在SharePoint 2013页面上使用脚本编辑器进行尝试。

4 个答案:

答案 0 :(得分:1)

很可能是因为Chrome拒绝为CORS请求设置Origin标头。它甚至不会让您显式覆盖Origin标头。基本上这会导致服务器看到Origin: null,在大多数情况下会导致403。 IE / Firefox显然没有这样的限制。

作为 SharePoint内部部署的解决方法,您可以在web.config中设置custom header

<customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>

或明确指定域名:

<customHeaders>
    <add name="Access-Control-Allow-Origin" value="http://anotherintra.contoso.com" />
</customHeaders>

答案 1 :(得分:0)

使用OOB脚本,它不会被修复。需要在Vadim Gremyachev指定的服务器端完成更改。它也可以在IE8中工作,但在IE10中,它会显示一个安全弹出窗口,要求访问其他域的数据。

答案 2 :(得分:0)

ValueError: Tensor conversion requested dtype string for Tensor with dtype float32: 'Tensor("arg0:0", shape=(), dtype=float32)'

值得一试: - )

答案 3 :(得分:0)

IE可能会工作而Chrome无法工作的原因可能是各个浏览器如何处理您的凭据。要在chrome中提供您的凭据,请在您的$.ajax调用中添加以下代码。

xhrFields: {
 withCredentials: true
},

请参见