0

I have written a successful crossdomain call using Rest which is hosted in an Menu Action App. It returns all document item properties. Ok now I think is the time to rewrite as a JavaScript" Promise. Could someone please take look at my code to see if I am on the write lines - allot of the samples I have seen seem to use $.ajax() whereas I have previously used a different syntax as shown below.

  // define our unique namespace for our functions.
 var WSL = window.Client.WSL || {};
  WSL.Rest = WSL.Rest || {};

  WSL.Rest.Lists = function () {


// REST Style promise functions
var getItemAllFields = function () {


    var deferred = $.Deferred();


    var executor;


    $('#message').text("REST Cross domain call");   // document.getElementById("message").innerText =

    // although we're fetching data from the host web, SP.RequestExecutor gets initialized with the app web URL..
    executor = new SP.RequestExecutor(appweburl);



    var urlItemAllFields = appweburl +
                  "/_api/SP.AppContextSite(@target)/web/getfilebyserverrelativeurl('" + itemurl + "')/ListItemAllFields?@target='" + hostweburl + "'";



    executor.executeAsync(
    {

        url: urlItemAllFields,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: Function.createDelegate(this, function (data, errorCode, errorMessage) { 
            //alert('Sucess');
            deferred.resove(data, errorCode,errorMessage);


        }),

        error:  Function.createDelegate(this, function (data, error, errorMessage) { deferred.reject(data, error, errorMessage); 


        }));




        return deferred.promise();



     };



return {

    getItemAllFields: getItemAllFields

 };


 }();
0

Those samples using $.ajax() are the ones which are working in a single domain whereas SP.RequestExecutor is mainly used to perform cross domain calls. The code above is making a REST call on Host Web from an App Web and as you may be aware they are in different domains hence the need for it. Note, that if your List is in App Web itself, then you can very well use $.ajax() instead of SP.RequestExecutor

UPDATE

The problem is you can't make a cross-domain call with AJAX because the same-origin policy will stop the call. That's why we have the cross-domain library in the first place. en.wikipedia.org/wiki/Same-origin_policy The only way to use ajax for cross-domain calls is with JSONP (GET only), or if the target supports Cross-Origin Resource Sharing (CORS)." On the positive side, jQuery.ajax returns a promise, so it's nicer for calls within the same origin.

  • ah that makes sense. In my case I do very little in the app web so it would have to be cross domain. Thanks – westerdaled Feb 9 '15 at 14:15
  • You can do GET requests using $.ajax() for cross-domain requests as well. You still need to use the /_api/SP.AppContextSite endpoint as you do with the RequestExecutor. – wjervis Feb 9 '15 at 14:23
  • 1
    @wjervis, that make sense, it essentially boils down to using RequestExecutor for getting the AppContextSite'. There is a choice in using either $.ajax()` or executor.executeAsync'. And this also mean not only can we do GET, we can make POST` calls as well using $.ajax() – Nadeem Yousuf Feb 9 '15 at 14:39
  • 1
    @NadeemYousuf, I did not know that you can make POST calls with $.ajax(). I always got an error, but that was probably me forgetting to add the request digest and other things that RequestExecutor automatically handled for you. – wjervis Feb 9 '15 at 15:25
  • 1
    all, I have made contact with Scott Hillier, wrt the SP.RequestExecutor version of my code which is far easier to understand than the $.ajax() call. I have published the main parts of the code to see if the syntax is ok. Should have an ans a bit later. – westerdaled Feb 10 '15 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.