locked
Multiple REST Calls with jQuery RRS feed

  • Question

  • Hi all,

    The data I need to display is in two separate REST calls. One to get Sites, another to check permissions on those sites. How would I include two REST calls using jQuery?

    Thanks,

    Kelly


    Personal Blog: http://thebitsthatbyte.com

    Wednesday, July 31, 2013 7:39 PM

Answers

  • According to your post, you can take advantage of $.when() and deferreds for this.

    Example1: Execute a function after two ajax requests are successful.

    $.when($.ajax("/page1.aspx"), $.ajax("/page2.aspx")).done(function(a1, a2){

      /* a1 and a2 are arguments resolved for the

          page1 and page2 ajax requests, respectively.

          each argument is an array with the following

          structure: [ data, statusText, jqXHR ] */

      var data = a1[0] + a2[0]; /* a1[0] = "Whip", a2[0] = " It" */

      if ( /Whip It/.test(data) ) {

        alert("We got what we came for!");

      }

    });

    Example2: Execute the function myFunc when both ajax requests are successful, or myFailure if either one has an error.

    $.when($.ajax("/page1.aspx "), $.ajax("/page2.aspx ")).then(myFunc, myFailure); 
    • Edited by Dennis Guo Friday, August 2, 2013 1:19 AM
    • Marked as answer by Kelly Rusk Friday, August 2, 2013 1:53 AM
    Friday, August 2, 2013 1:06 AM
  • Its simple ... make the first Ajax call and register a success handler, then in that success method make the second Ajax call

    val it: unit=()

    • Marked as answer by Kelly Rusk Thursday, August 1, 2013 4:30 AM
    Thursday, August 1, 2013 3:35 AM

All replies

  • When you call REST web services from Javascript using Ajax. the call is made async. So the only way in which you can make sure that the "check permissions on those sites" works after the "get sites" ... is that you call the "check permissions on those sites" in the Success method of the call to "get sites"



    val it: unit=()

    Thursday, August 1, 2013 1:59 AM
  • Thank you. Do you have any examples or links?

    Personal Blog: http://thebitsthatbyte.com

    Thursday, August 1, 2013 2:25 AM
  • Its simple ... make the first Ajax call and register a success handler, then in that success method make the second Ajax call

    val it: unit=()

    • Marked as answer by Kelly Rusk Thursday, August 1, 2013 4:30 AM
    Thursday, August 1, 2013 3:35 AM
  • According to your post, you can take advantage of $.when() and deferreds for this.

    Example1: Execute a function after two ajax requests are successful.

    $.when($.ajax("/page1.aspx"), $.ajax("/page2.aspx")).done(function(a1, a2){

      /* a1 and a2 are arguments resolved for the

          page1 and page2 ajax requests, respectively.

          each argument is an array with the following

          structure: [ data, statusText, jqXHR ] */

      var data = a1[0] + a2[0]; /* a1[0] = "Whip", a2[0] = " It" */

      if ( /Whip It/.test(data) ) {

        alert("We got what we came for!");

      }

    });

    Example2: Execute the function myFunc when both ajax requests are successful, or myFailure if either one has an error.

    $.when($.ajax("/page1.aspx "), $.ajax("/page2.aspx ")).then(myFunc, myFailure); 
    • Edited by Dennis Guo Friday, August 2, 2013 1:19 AM
    • Marked as answer by Kelly Rusk Friday, August 2, 2013 1:53 AM
    Friday, August 2, 2013 1:06 AM