Skip to content
Phil Brown edited this page Jul 13, 2013 · 6 revisions

Home/API Documentation/Ajax

Ajax is used to perform network tasks on Android. A long-hand Ajax task can be started with any of the four methods:

ajax(Map<String, Object> options)

Accepts a key-value pairing of Ajax Options. For example:

ajax($.map($.entry("context", this), $.entry("url", "http://www.example.com"), $.entry("type", "GET"), $.entry("dataType", "TEXT"), $.entry("success", new Function(){
    @Override
    public void invoke($ droidQuery, Object... params) {
        droidQuery.alert((String) params[0]);
    }
})));

ajax(String options)

Accepts a JSON String of Ajax Options. For example:

String data = "{id: 1, text: 'foobar'}";
ajax("{url: 'http://www.example.com', type: 'POST', dataType: 'JSON', data: '" + data +"'}");

ajax(JSONObject options)

Accepts a JSON Object of Ajax Options. For example:

JSONObject json = new JSONObject();
json.put("url", "http://www.example.com");
json.put("type", "DELETE");
json.put("dataType", "XML");
json.put("context", this);
json.put("success", new Function() {
    @Override
    public void invoke($ droidQuery, Object... params) {
        $.alert("Success!");
    }
});
ajax(json);

ajax(AjaxOptions options)

Accepts an AjaxOptions Object to configure the request. For example:

ajax(new AjaxOptions().url("http://www.example.com")
                      .type("GET")
                      .dataType("Script")
                      .context(this).success(new Function() {
                          @Override
                          public void invoke($ droidQuery, Object... params) {
                              ScriptResponse response = (ScriptResponse) params[0];
                              droidQuery.alert(response.output);
                          }
                      }).error(new Function() {
                          @Override
                          public void invoke($ droidQuery, Object... params) {
                              Log.e("$", "Could not complete ajax request");
                          }
                      }));