예제 #1
0
파일: Ajax.java 프로젝트: hoota/strictweb
 public void setRequestHeaders(XMLHttpRequest request) {
   //		request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   request.setRequestHeader("Content-type", "application/json");
 }
예제 #2
0
파일: Ajax.java 프로젝트: hoota/strictweb
  Object makeRequest(
      boolean async, final List<AjaxRequest> requests, final VoidDelegate<Throwable> errorHandler) {
    onError(null);

    XMLHttpRequest request = getHttpRequest();
    //        String postXml = "";
    //
    //        for(int i=0; i<requests.size(); i++) {
    //            AjaxRequest r = requests.get(i);
    //	        if(r.errorHandler != null) r.errorHandler.voidDelegate(null);
    //	        postXml += "&"+XML_DATA_PARAM+i+"="
    //	            + objectToXml(r.args, null).replaceAll("%", "%25").replaceAll("&",
    // "%26").replaceAll(";", "%3B").replaceAll("\\+", "%2B");
    //	    }

    String postJson = "[";
    for (int i = 0; i < requests.size(); i++) {
      AjaxRequest r = requests.get(i);
      postJson += objectToJson(r.clazz) + "," + objectToJson(r.method) + "," + arrayToJson(r.args);
      if (i != requests.size() - 1) postJson += ",";
    }
    postJson += "]";

    final String url = getRequestUrl(requests);

    final Node[] eventTargetNodes = new Node[3];

    eventTargetDisable(eventTargetNodes);

    if (async) {
      StrictWeb.setVoidEventCallback(
          request,
          EV_ONREADYSTATECHANGE,
          new VoidDelegate<XMLHttpRequest>() {
            public void voidDelegate(XMLHttpRequest request) {
              if (request.readyState == 4) {
                try {
                  StrictWeb.setVoidEventCallback(request, EV_ONREADYSTATECHANGE, null);
                } catch (Exception e) {
                  /*StrictWeb.window.alert(e);*/
                }
                eventTargetEnable(eventTargetNodes);

                if (request.status == 200) {
                  AjaxRequestResult[] results = parseRequestResult(request, url, requests);
                  for (int i = 0; i < results.length; i++) {
                    AjaxRequestResult res = results[i];
                    AjaxRequest req = requests.get(i);
                    Throwable error = res.getError();
                    if (error != null) {
                      throwError(
                          "Ajax.call(" + req.method + "): server-side exception",
                          error,
                          req.errorHandler);
                    } else {
                      req.callBack.voidDelegate(res.data);
                    }
                  }
                } else {
                  throwError("Error status: " + request.status, null, errorHandler);
                }
              }
            }
          });
    }

    //		postXml = getQueryString(requests) + postXml;
    request.open("POST", url, async, null, null);

    Log.info("POST URL : " + url);
    Log.info("POST JSON: " + postJson);
    Log.info("POST args:");
    Log.info(requests);

    setRequestHeaders(request);
    request.send(postJson);

    if (async) return null;

    // doing sync parse
    eventTargetEnable(eventTargetNodes);
    AjaxRequestResult res = parseRequestResult(request, url, requests)[0];
    AjaxRequest req = requests.get(0);
    Throwable error = res.getError();
    if (error != null) {
      throwError("Ajax.call(" + req.method + "): server-side exception", error, req.errorHandler);
    }
    return res.data;
  }