Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010 Google Inc.
   *
   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
   * use this file except in compliance with the License. You may obtain a copy of
   * the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  * License for the specific language governing permissions and limitations under
  * the License.
  */
 package elemental.js.util;
 
 
 
 
 public class Xhr {
  
Interface for getting notified when an XHR successfully completes, or errors out.
 
   public interface Callback {
     void onFail(XMLHttpRequest xhr);
 
     void onSuccess(XMLHttpRequest xhr);
   }
 
   private static class Handler implements ReadyStateChangeHandler {
     private final Callback callback;
 
     private Handler(Callback callback) {
       this. = callback;
     }
 
     public void onReadyStateChange(XMLHttpRequest xhr) {
       if (xhr.getReadyState() == .) {
         if (xhr.getStatus() == 200) {
           .onSuccess(xhr);
           xhr.clearOnReadyStateChange();
           return;
         }
         .onFail(xhr);
         xhr.clearOnReadyStateChange();
       }
     }
   }

  
Send a GET request to the url and dispatch updates to the callback.

Parameters:
url
callback
 
   public static void get(String urlCallback callback) {
     request(create(), "GET"urlcallback);
   }

  
Send a GET request to the url and dispatch updates to the callback.

Parameters:
window the window object used to access the XMLHttpRequest constructor
url
callback
 
   public static void get(Window windowString urlCallback callback) {
     request(create(window), "GET"urlcallback);
   }

  
Send a HEAD request to the url and dispatch updates to the callback.

Parameters:
url
callback
 
   public static void head(String urlCallback callback) {
     request(create(), "HEAD"urlcallback);
   }

  
Send a HEAD request to the url and dispatch updates to the callback.

Parameters:
window the window object used to access the XMLHttpRequest constructor
url
callback
  public static void head(Window windowString urlCallback callback) {
    request(create(window), "HEAD"urlcallback);
  }

  
Send a POST request to the url and dispatch updates to the callback.

Parameters:
url
requestData the data to be passed to XMLHttpRequest.send
contentType a value for the Content-Type HTTP header
callback
  public static void post(String urlString requestDataString contentTypeCallback callback) {
    request(create(), "POST"urlrequestDatacontentTypecallback);
  }

  
Send a POST request to the url and dispatch updates to the callback.

Parameters:
window the window object used to access the XMLHttpRequest constructor
url
requestData the data to be passed to XMLHttpRequest.send
contentType a value for the Content-Type HTTP header
callback
  public static void post(
      Window windowString urlString requestDataString contentTypeCallback callback) {
    request(create(window), "POST"urlrequestDatacontentTypecallback);
  }
  private static XMLHttpRequest create() {
    return create(Browser.getWindow());
  }

  
Replacement for com.google.gwt.xhr.client.XMLHttpRequest.create() that allows better control of which window object is used to access the XMLHttpRequest constructor.
  private static native XMLHttpRequest create(Window window/*-{
    return new window.XMLHttpRequest();
  }-*/;
  private static void request(XMLHttpRequest xhr,
      String method,
      String url,
      String requestData,
      String contentType,
      Callback callback) {
    try {
      xhr.setOnReadyStateChange(new Handler(callback));
      xhr.open(methodurl);
      xhr.setRequestHeader("Content-type"contentType);
      xhr.send(requestData);
    } catch (JavaScriptException e) {
      // Just fail.
      callback.onFail(xhr);
      xhr.clearOnReadyStateChange();
    }
  }
  private static void request(XMLHttpRequest xhr,
      String method,
      String url,
      final Callback callback) {
    try {
      xhr.setOnReadyStateChange(new Handler(callback));
      xhr.open(methodurl);
      xhr.send();
    } catch (JavaScriptException e) {
      // Just fail.
      callback.onFail(xhr);
      xhr.clearOnReadyStateChange();
    }
  }
New to GrepCode? Check out our FAQ X