SharePoint CORS fix

This is possible fix for the a possible calls made from one application to your SharePoint side that is located in another domain.

In your SharePoint application add the following custom headers to enable requests from a different domain. Just change the header: Access-Control-Allow-Origin to point to the application who is making a call to your SharePoint application.

<system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name=”Access-Control-Allow-Credentials” value=”true” />
        <add name=”Access-Control-Allow-Origin” value=”http://source-application.com​&#8221; />
        <add name=”Access-Control-Request-Method” value=”GET,POST,HEAD,OPTIONS” />
      </customHeaders>
    </httpProtocol>​
</system.webServer>​

Also if you are making an AJAX call add the following parameter to the call:
xhrFields: { withCredentials: true }

 

Sample code:

jQuery.ajax({
 url: requestUri,
 type: 'GET',
 dataType: 'json',
 headers: requestHeaders,
 xhrFields: { withCredentials: true },
 success: function (data)
 { 

 },
 error: function ajaxError(response) {
 console.log(response.status + ' ' + response.statusText);
 }
 });

Other possible parameters for the AJAX call:

crossDomain: true
And for CORS support:
$.support.cors = true;

A bit more detail can be found here in this post:
https://blog.kenaro.com/2014/03/18/cross-site-scripting-with-sharepoint-2013-rest-calls/

SharePoint REST Service ListData.svc and JQuery

This is a sample code on JQuery how you can retrieve a value from a SP 2010 REST service and display it in a HTML element.

In this case this is a piece of code that was used with a blog post xsl output.

There are 3 parts in this code:

1.  Find out the relative URL of the site in the current SP site

var location = window.location.pathname;
location = location.replace(“Lists/Posts/Post.aspx”, “”);

2.  Make a ajax call to the “ListData.svc” service. In this example “Posts” is the name of the posts library in a Blog site. The GetQueryStringParams is a JS function that retrieves the post (list item) ID to be passed to the service and the “LikesCounter” is the field which value we want to get.
$j.ajax({
type: “GET”,
url: location + “/_vti_bin/ListData.svc/Posts(” + GetQueryStringParams(“ID”) + “)/LikesCounter”,
dataType: “xml”,
success: parseXmlLikesCounter
});

URL Samle for the AJAX call:

http://servername:222/support/news/_vti_bin/ListData.svc/Posts(3)/LikesCounter

3. Take the value from the service. Strip it of any additional XML elements and get the value. In this case the single value is going to contain some extra XML defining the type of the data etc. I did not need that so I stripped any extra info and kept only the data from the field.
function parseXmlLikesCounter(xml) {
$j(xml).find(“Counter”).each(function () {

$j(“#ILikeItButtonValue”).text($j(this).text());
});
}

XML Samle of the response from the service:

<?xml version=”1.0″ encoding=”utf-8″ standalone=”yes” ?>
<LikesCounter p1:type=”Edm.Double” xmlns:p1=”http://schemas.microsoft.com/ado/2007/08/dataservices/metadata&#8221; xmlns=”http://schemas.microsoft.com/ado/2007/08/dataservices”>1</LikesCounter&gt;

More info on the REST service in SharePoint:

http://msdn.microsoft.com/en-us/library/ff798339.aspx