开发者

GWT Request Builder is not working in production but working in development

开发者 https://www.devze.com 2023-03-30 17:18 出处:网络
I\'m using GWT and creating a HTTP request but I\'m having issues accessing the file from the production version, even though it\'s working fine in development. My main program has the following for t

I'm using GWT and creating a HTTP request but I'm having issues accessing the file from the production version, even though it's working fine in development. My main program has the following for the request on the client side.

static final String dataURL = GWT.getModuleBaseURL() + "interpretData";

public void onModuleLoad() {
  requestData(dataURL, new AsyncCallback<String>() {
        public void onFailure(Throwable caught) {
            RootPanel.get(holderId).add(new Label(error + ": Asynchronous call failed - " + caught.getLocalizedMessage()));
            return;
        }

    public void onSuccess(String JSON){
        try{
                  // code executed on success
        } catch (Exception e) {
            RootPanel.get(holderId).add(new Label(error + ": " + e.getMessage()));
            return;
        }
    }
  });
}

public static void requestData(final String url, final AsyncCallback<String> callback) {
      // create a request for the xml data on the server
    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
    builder.setCallback(new RequestCallback() {
        public void onError(Request request, Throwable exception) {
            callback.onFailure(exception);
        }

        public void onResponseReceived(Request request, Response response) {
            try {
                final int responseCode = response.getStatusCode() / 100;
                if (url.startsWith("file:/") || (responseCode == 2) || (responseCode == 0)){
                    callback.onSuccess(response.getText());
                } else {
                    callback.onFailure(new IllegalStateException(" Http Error: #" + response.getStatusCode() + " - " + response.getStatusText()));  
                }
            } catch (Throwable e) {
                callback.onFailure(e);
            }           
        }
    });

On the server side, I have:

public class interpretData extends HttpServlet {

    public void doGet(HttpServletRequest requ开发者_StackOverflowest, HttpServletResponse response) throws ServletException, IOException{

        response.setContentType("application/json");
            // code to return a String
}

Finally, my XML file has the following in it:

  <servlet>
    <servlet-name>interpretData</servlet-name>
    <servlet-class>com.gmod.caeli.server.interpretData</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>interpretData</servlet-name>
    <url-pattern>/caeli/interpretData</url-pattern>
  </servlet-mapping>

In the end, I can access the file from: http://127.0.0.1:8888/caeli/interpretData so the development version is completely fine, but I don't know how to get it to work in production (the URL I'm calling for production is file:///~/workspace/Caeli/war/caeli/interpretData) I've searched for examples, but I haven't found any clues to what I'm doing wrong. I tried using setting it up with tomcat and I got a 404 error there too. I feel like I'm missing something small, so hopefully this is enough information for someone to notice something wrong.


From my experience and research, the URL that your attempting to request in production (file:///...) cannot be requested by the web browser via an Ajax call, anchor tag, javascript etc. It might be a little confusing/misleading as you can enter that URL into your browser manually and get the expected result, however this local resource request is not allowed by the browser.

0

精彩评论

暂无评论...
验证码 换一张
取 消