aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManuel Carrasco Moñino <manuel.carrasco.m@gmail.com>2013-12-30 21:00:47 +0100
committerManuel Carrasco Moñino <manuel.carrasco.m@gmail.com>2013-12-30 21:00:47 +0100
commit26d30bf88f643c043768960d356c4f7e84e5667d (patch)
treed86fe1304527e4618825a449b00e05ed6905492a
parent93b8d5792dfe60014cfbaaaec0a17ed0111ebccd (diff)
downloadgwtquery-26d30bf88f643c043768960d356c4f7e84e5667d.tar.gz
gwtquery-26d30bf88f643c043768960d356c4f7e84e5667d.zip
Fix some issues related with FormData submit
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java69
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilder.java29
2 files changed, 49 insertions, 49 deletions
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java
index 9e99f3b5..97a95996 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java
@@ -2,6 +2,7 @@ package com.google.gwt.query.client.plugins.ajax;
import com.google.gwt.core.client.Callback;
import com.google.gwt.core.client.GWT;
+import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.ScriptInjector;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.ScriptElement;
@@ -119,23 +120,22 @@ public class Ajax extends GQuery {
onError.setElement(settings.getContext());
}
- String httpMethod = settings.getType() == null ? "POST" : settings.getType().toUpperCase();
- Object data = resolveData(settings, httpMethod);
- final String url = resolveUrl(settings, httpMethod, data);
+ resolveSettings(settings);
+
final String dataType = settings.getDataType();
Promise ret = null;
if ("jsonp".equalsIgnoreCase(dataType)) {
- ret = new PromiseReqBuilderJSONP(url, null, settings.getTimeout());
+ ret = new PromiseReqBuilderJSONP(settings.getUrl(), null, settings.getTimeout());
} else if ("loadscript".equalsIgnoreCase(dataType)){
- ret = createPromiseScriptInjector(url);
+ ret = createPromiseScriptInjector(settings.getUrl());
} else {
- ret = new PromiseReqBuilder(settings, httpMethod, url, data)
+ ret = new PromiseReqBuilder(settings)
.then(new Function() {
public Object f(Object...args) {
- Response response = (Response)args[0];
- Request request = (Request)args[1];
+ Response response = arguments(0);
+ Request request = arguments(1);
Object retData = null;
try {
if ("xml".equalsIgnoreCase(dataType)) {
@@ -172,7 +172,29 @@ public class Ajax extends GQuery {
return ret;
}
+ private static void resolveSettings(Settings settings) {
+ String url = settings.getUrl();
+ assert settings != null && settings.getUrl() != null: "no url found in settings";
+
+ settings.setType(settings.getType() == null ? "POST" : settings.getType().toUpperCase());
+
+ Binder data = settings.getData();
+ if (data != null) {
+ if (data.getBound() instanceof JavaScriptObject && JsUtils.isFormData(data.<JavaScriptObject>getBound())) {
+ settings.setDataString(null);
+ } else if (settings.getType().matches("(POST|PUT)") && "json".equalsIgnoreCase(settings.getDataType())) {
+ settings.setDataString(data.toJson());
+ } else {
+ settings.setDataString(data.toQueryString());
+ }
+ }
+ if ("GET".equals(settings.getType()) && settings.getDataString() != null) {
+ url += (url.contains("?") ? "&" : "?") + settings.getDataString();
+ settings.setUrl(url);
+ }
+ }
+
private static Promise createPromiseScriptInjector(final String url) {
return new PromiseFunction() {
private ScriptElement scriptElement;
@@ -194,33 +216,6 @@ public class Ajax extends GQuery {
};
}
- private static String resolveUrl(Settings settings, String httpMethod, Object data) {
- String url = settings.getUrl();
- assert url != null : "no url found in settings";
- if ("GET".equals(httpMethod) && data instanceof String) {
- url += (url.contains("?") ? "&" : "?") + data;
- }
- return url;
- }
-
- private static Object resolveData(Settings settings, String httpMethod) {
- Object data = settings.getDataString();
- Binder sdata = settings.getData();
- if (data == null && sdata != null) {
- String type = settings.getDataType();
- if (type != null
- && (httpMethod.matches("(POST|PUT)"))
- && type.equalsIgnoreCase("json")) {
- data = sdata.toString();
-// } else if (JsUtils.isFormData(sdata)) {
-// data = sdata;
- } else {
- data = sdata.toQueryString();
- }
- }
- return data;
- }
-
public static Promise ajax(String url, Function onSuccess, Function onError) {
return ajax(url, onSuccess, onError, (Settings) null);
}
@@ -334,6 +329,10 @@ public class Ajax extends GQuery {
.setSuccess(success)
);
}
+
+ public static Promise post(Settings s) {
+ return ajax(s);
+ }
public static Promise post(String url, Properties data) {
return post(url, data, null);
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilder.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilder.java
index 3d1d0194..5b687e54 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilder.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilder.java
@@ -64,7 +64,13 @@ public class PromiseReqBuilder extends DeferredPromiseImpl implements RequestCal
* which are not available in GWT, like adding progress handles or sending
* javascript data (like forms in modern html5 file api)
*/
- public PromiseReqBuilder(Settings settings, String httpMethod, String url, Object data) {
+ public PromiseReqBuilder(Settings settings) {
+ String httpMethod = settings.getType();
+ String url = settings.getUrl();
+ Binder data = settings.getData();
+ String ctype = settings.getContentType();
+ Boolean isFormData = data != null && data.getBound() instanceof JavaScriptObject && JsUtils.isFormData(data.<JavaScriptObject>getBound());
+
XMLHttpRequest xmlHttpRequest = XMLHttpRequest.create();
try {
if (settings.getUsername() != null && settings.getPassword() != null) {
@@ -86,7 +92,7 @@ public class PromiseReqBuilder extends DeferredPromiseImpl implements RequestCal
JsCache p = arguments(0);
double total = p.getDouble("total");
double loaded = p.getDouble("loaded");
- double percent = 100 * loaded / total;
+ double percent = loaded == 0 ? 0 : total == 0 ? 100 : (100 * loaded / total);
dfd.notify(total, loaded, percent, "download");
}
}));
@@ -109,17 +115,12 @@ public class PromiseReqBuilder extends DeferredPromiseImpl implements RequestCal
}
}
- if (data != null && !"GET".equalsIgnoreCase(httpMethod)) {
- String ctype = settings.getContentType();
- if (data instanceof JavaScriptObject && JsUtils.isFormData((JavaScriptObject)data)) {
- ctype = FormPanel.ENCODING_MULTIPART;;
- } else if (ctype == null) {
- String type = settings.getDataType();
- if (type != null && type.toLowerCase().startsWith("json")) {
- ctype = "application/json; charset=utf-8";
- } else {
- ctype = FormPanel.ENCODING_URLENCODED;
- }
+ if (data != null && !isFormData && !"GET".equalsIgnoreCase(httpMethod)) {
+ String type = settings.getDataType();
+ if (type != null && type.toLowerCase().startsWith("json")) {
+ ctype = "application/json; charset=utf-8";
+ } else {
+ ctype = FormPanel.ENCODING_URLENCODED;
}
xmlHttpRequest.setRequestHeader("Content-Type", ctype);
}
@@ -140,7 +141,7 @@ public class PromiseReqBuilder extends DeferredPromiseImpl implements RequestCal
});
try {
- JsUtils.runJavascriptFunction(xmlHttpRequest, "send", data);
+ JsUtils.runJavascriptFunction(xmlHttpRequest, "send", isFormData ? data.getBound() : settings.getDataString());
} catch (JavaScriptException e) {
onError(null, e);
}