简体   繁体   中英

JQuery Json Access of a Sharepoint List via Rest

I try to access a sharepoint list via Jquery and REST Interface. The Site with the code below is running 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:59936 is not allowed by Acess-Control-Allow-Origin."

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?

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

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.

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/

In regards to 3nigma's answer. Jquery's crossDomain flag won't work, because the Sharepoint services aren't designed as JSONP services, 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.)

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:

$.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.

<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>

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:

  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.

Thanks,

Matt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM