javascript JQuery Json 通过 Rest 访问共享点列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6855929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:07:20  来源:igfitidea点击:

JQuery Json Access of a Sharepoint List via Rest

javascriptsharepointrestjquery

提问by murratore

I try to access a sharepoint list via Jquery and REST Interface. The Site with the code below is running localhost.

我尝试通过 Jquery 和 REST 接口访问共享点列表。带有以下代码的站点正在运行 localhost。

This code doesn't work:

此代码不起作用:

$(document).ready(function() { getdata(); });

function getdata() {

    alert("start");

    $.ajax({
        url: "http://spkerberostest.vz.ch/_vti_bin/ListData.svc/Tasks",
        dataType: 'JSON',
        success:function(json) { alert ("Success");
        },
        error:function(){
            alert("Error");
        }
    });        
};

I get the Error Message "Origin http://localhost:59936is not allowed by Acess-Control-Allow-Origin."

我收到错误消息“ Acess-Control-Allow-Origin 不允许Origin http://localhost:59936。”

I'm not sure what the reason is. Is it the fact that Sharepoint needs Authentication (anonymous is blocked) or is it the cross-domain call? Or even both?

我不确定是什么原因。是 Sharepoint 需要身份验证(匿名被阻止)还是跨域调用?或者两者兼而有之?

What can I do? I read somewhere about JSONP as Data Type. But this didn't work. Thanks.

我能做什么?我在某处阅读了有关 JSONP 作为数据类型的信息。但这没有用。谢谢。

回答by Chris Jaynes

Assuming that both of these resources are internal to your company, and you always access one from the other, your Sharepoint administrator could try to turn on what are called CORS (Cross Origin Resource Sharing) headers on the Sharepoint IIS servers.

假设这两个资源都是您公司的内部资源,并且您总是从另一个访问一个,您的 Sharepoint 管理员可以尝试打开 Sharepoint IIS 服务器上所谓的 CORS(跨源资源共享)标头。

This will allow your cross-origin calls to complete successfully, as the browser and the servers exchange headers requesting cross-origin sharing. You can learn more about CORS at http://enable-cors.org/

这将允许您的跨域调用成功完成,因为浏览器和服务器交换请求跨域共享的标头。您可以在http://enable-cors.org/ 上了解有关 CORS 的更多信息

In regards to 3nigma's answer. Jquery's crossDomain flag won't work, because the Sharepoint services aren't designed as JSONPservices, which is what Jquery attempts to use when you set that flag. (The Sharepoint server would have to pad the data like it was a Javascript file with a single JSON object in it, but I don't know of a way to configure it to do that.)

关于 3nigma 的回答。Jquery 的 crossDomain 标志将不起作用,因为 Sharepoint 服务并非设计为JSONP服务,而这正是您设置该标志时 Jquery 尝试使用的。(Sharepoint 服务器必须填充数据,就像它是一个包含单个 JSON 对象的 Javascript 文件,但我不知道如何配置它来做到这一点。)

回答by Russellg

Just in case anyone else is hiting this issue, I got this working in my environment by setting the jquery, suoport.cors to true. the code is:

以防万一其他人遇到这个问题,我通过将 jquery、suoport.cors 设置为 true 来使其在我的环境中工作。代码是:

$.support.cors = true;
$.ajax({
crosDomain:true,    
    url: listUrl,
    success: getItemsSuccess,
    error: jqueryError,
    dataType:'json'

});

this allows me to access a list on another physical server. No changes to iis were required. No JSONP needed.

这允许我访问另一台物理服务器上的列表。不需要更改 iis。不需要 JSONP。

回答by DerDer73

<script type="text/javascript">
$(document).ready(function() { getdata(); });

function getdata() {

    alert("start");

    $.ajax({
        url: "http://yourserver/_api/Web/Lists/getByTitle('yourlist
')/items/",
        type: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
        },
        success:function(json) { alert ("Success");
        },
        error:function(){
            alert("Error");
        }
    });        
};
</script>

回答by Matt Cashatt

You need to instead call your own server, and then have your server call the SharePoint Server. Assuming that you are using a C# middle-tier, it would look something like this:

您需要改为调用您自己的服务器,然后让您的服务器调用 SharePoint Server。假设您使用的是 C# 中间层,它看起来像这样:

  public string getJson()
    {
        WebClient wc = new WebClient();
        wc.Credentials = new System.Net.NetworkCredential("[user]", "[password]", "[domain]");
        var url = "[some url in the 12 hive that can return json]";

        var result = wc.DownloadString(url);

        return result;
    }

Of course you need to add code for outputting the json back to your client, but the code above is how you can get the SP data you need.

当然你需要添加将json输出回客户端的代码,但是上面的代码是你如何获取你需要的SP数据。

Thanks,

谢谢,

Matt

马特