Use of success / jsonpCallback with ajax request

后端 未结 2 675
忘掉有多难
忘掉有多难 2020-12-10 07:33

I\'m developing an application for Netflix using their OData API. I\'ve followed Stephen Walther\'s blog entry on how to query the OData API. In it, he uses the following co

2条回答
  •  忘掉有多难
    2020-12-10 07:58

    Don't define callback, because jQuery creates that function for you. Here's an example, jsFiddle:

    function getTitles() {
        query = "http://odata.netflix.com/v2/Catalog" // netflix odata base url
        + "/Genres('Television')" // select Genre
        + "/Titles" // top-level resource
        + "?$select=NetflixApiId,Name,BoxArt,Synopsis,ReleaseYear,AverageRating,Series" // choose fields 
        + "&$orderby=Name" // Sort results by name
        + "&$filter=Instant/Available eq true"  // filter by instant view
        + " and Type eq 'Season'" // select only seasons
        + "&$expand=Series" // include series data
        + "&$callback=?" // specify name of callback function
        + "&$format=json"; // json request
        $.ajax({
          dataType: "jsonp",
          url: query,
          success: callback,
          error: function(XHR, textStatus, errorThrown){
            alert(textStatus + ":" + errorThrown);
          } 
        });
      }
    

    callback=? basically asks jQuery to handle all the JSONP returns.

提交回复
热议问题