简体   繁体   中英

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

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 request, 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.

根据我的经验和研究,Web浏览器无法通过Ajax调用,锚标记,javascript等来请求您尝试在生产环境中请求的URL(文件:/// ...)。这可能有点令人困惑/可以误导您,因为您可以将该URL手动输入浏览器并获得预期的结果,但是浏览器不允许此本地资源请求。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM