Hi Arnaud, This is actually a limitation of the JSONP technique. Since the essence of the technique is to inject <script> tags, you are constrained to only being able to issue GET requests to any server component listening for the request.
However, as you will find other developers doing online, you could try tunneling the GET to a POST on the server-side. There are even some libraries that could do this for you that should turn up in a web search for JSONP POST terms or the like. Hope that helps, -Sumit Chandel On Tue, Aug 11, 2009 at 7:21 AM, Arnaud <[email protected]> wrote: > > > Thanks for the code. This actually covers well the GET part, but as > you say, not the POST. > Thanks cause it is still very interesting implementation. > > Does anybody has written or seen around a similar code that would also > support POST ? > > Thanks for the help. > > Arnaud > > > > On 10 ago, 14:38, MiSt <[email protected]> wrote: > > Inspired by someone's example I don't remember where I've found it. > > This method works only with GET > > > > Example: > > > > CrossSiteJsonRequestUtil.INSTANCE.doFetchURL("www.test.com", > > "callbackmethodname", new AsyncCallback<JSONObject>() { > > > > @Override > > public void onFailure(Throwable caught) { > > // TODO > > > > } > > > > @Override > > public void onSuccess(JSONObject result) { > > // TODO > > > > } > > > > }); > > > > import java.util.HashMap; > > import java.util.Map; > > > > import com.google.gwt.core.client.JavaScriptObject; > > import com.google.gwt.json.client.JSONObject; > > import com.google.gwt.user.client.rpc.AsyncCallback; > > > > public class CrossSiteJsonRequestUtil { > > > > public static CrossSiteJsonRequestUtil INSTANCE = new > > CrossSiteJsonRequestUtil(); > > > > private Map<String, AsyncCallback<JSONObject>> callbacks = new > > HashMap<String, AsyncCallback<JSONObject>>(); > > > > private int curIndex = 0; > > > > private static final String CALLBACK_PARAM_NAME = "callback"; > > > > public void doFetchURL(String baseUrl, > > AsyncCallback<JSONObject> responseHandler) { > > doFetchURL(baseUrl, CALLBACK_PARAM_NAME, responseHandler); > > } > > > > public void doFetchURL(String baseUrl, String callbackParamName, > > AsyncCallback<JSONObject> responseHandler) { > > String callbackName = reserveCallback(responseHandler); > > setup(callbackName); > > > > char sep; > > if (baseUrl.contains("?")) { > > sep = '&'; > > } else { > > sep = '?'; > > } > > > > addScript(callbackName, baseUrl + sep + callbackParamName + "=" > > + callbackName); > > > > } > > > > private void setup(String callback) { > > setup(this, callback); > > } > > > > private String reserveCallback(AsyncCallback<JSONObject> > > responseHandler) { > > while (true) { > > if (!callbacks.containsKey("__gwt_callback" + curIndex)) { > > callbacks.put("__gwt_callback" + curIndex, > responseHandler); > > return "__gwt_callback" + curIndex++; > > } > > } > > } > > > > private native static void setup(CrossSiteJsonRequestUtil > > instance, > > String callback) /*-{ > > window[callback] = function(someData) { > > instan...@crosssitejsonrequestutil::handle(Lcom/google/gwt/ > > core/client/JavaScriptObject;Ljava/lang/String;)(someData,callback); > > var scriptEl = document.getElementById(callback); > > document.getElementsByTagName("body")[0].removeChild > > (scriptEl); > > window[callback] =null; > > } > > }-*/; > > > > private native void addScript(String uniqueId, String url) /*-{ > > var elem = document.createElement("script"); > > elem.setAttribute("language", "JavaScript"); > > elem.setAttribute("src", url); > > elem.setAttribute("id", uniqueId); > > document.getElementsByTagName("body")[0].appendChild(elem); > > }-*/; > > > > @SuppressWarnings("unused") > > private void handle(JavaScriptObject jso, String callback) { > > JSONObject jsonObject = null; > > AsyncCallback<JSONObject> responseHandler = > callbacks.get(callback); > > try { > > jsonObject = new JSONObject(jso); > > } catch (RuntimeException e) { > > responseHandler.onFailure(e); > > } > > if (jsonObject != null) { > > responseHandler.onSuccess(jsonObject); > > } > > > > } > > > > } > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/Google-Web-Toolkit?hl=en -~----------~----~----~----~------~----~------~--~---
