1

As of 10.04.2012, There is a short paragraph in the Facebook developer document for 'batch request' entitled: Batch calls with JSONP, which reads:

"The Batch API supports JSONP, just like the rest of the Graph API - 
 the JSONP callback function is specified using the 'callback' query string 
 or form post parameter."

I thought that meant you can also do a batch request using JSONP from Javascript (which will be a GET request, as JSONP works only as a GET request), so I tried that, with adding a 'batch' parameter (containing objects describing requests for batch as in the doc) to the query string.
Response from FB server was:

Only POST is allowed for batch requests

So, questions:
1. What did they mean in that paragraph?
2. Is there a way to do an asynchronous batch request from Javascript?

4

1 回答 1

0

我也一样。示例代码是

jQuery.support.cors = true;     
var AjaxRequest = jQuery.ajax( {
  url: "http://graph.facebook.com/",
  type: "POST",
  contentType: "application/x-www-form-urlencoded",
  data: { "access_token": AccessToken, "batch": BatchRequest },
  dataType: "jsonp",
  error: function( jqXHR, textStatus, errorThrown ) {
    ... show error stuff
    },
  success: function( Return, textStatus, jqXHR ) {
    showLog( "Return " + JSON.stringify( Return ) );
    showLog( "textStatus " + textStatus );
    showLog( "jqXHR " + JSON.stringify( jqXHR ) );
    if ( Return.error ) {
      ... go away
      }
    else {
      ... use the data
      }
    }
  } ); // eo ajax request

这使

Return {"error":3,"error_description":"Only POST is allowed for batch requests"}
textStatus success
jqXHR {"readyState":4,"status":200,"statusText":"success"}

即它成功地发回了一条错误消息。JSONP 将 POST 类型转换为 Facebook 不支持的 GET...

要回答 qu.2,您可以使用 FB.api 在 javascript 中执行异步批处理请求。我正在尝试 JSONP,因为 IE8 一直在使用 FB.api 从 Facebook 返回。

于 2012-05-10T18:02:34.797 回答