JavaFX WebView does not receive 200 messages from AJAX

0

I'm using the Restlet library that allows you to use RESTful services without needing an external server.

WebView and Browser code:

public class View extends Application {
public static void begin(String args[]) {
    launch(args);
}

public void start(Stage stage) throws Exception {
    stage.setTitle("Cert View");
    scene = new Scene(new Browser(), 1400, 900, Color.web("#666970"));
    stage.setScene(scene);
    stage.show();
}

private Scene scene;
}

class Browser extends Region {
final WebView browser = new WebView();
final WebEngine webEngine;
final VBox vbox = new VBox();
final String pagina = "br/com/softbox/eldoc/util/resources/html/index.html";

public Browser() {
    webEngine = browser.getEngine();
    vbox.getChildren().add(browser);
    getStyleClass().add("browser");
    webEngine.load(getClass().getClassLoader().getResource(pagina).toString());
    getChildren().add(browser);
}

protected void layoutChildren() {
    double w = getWidth();
    double h = getHeight();
    layoutInArea(browser, 0.0, 0.0, w, h, 0.0, HPos.CENTER, VPos.CENTER);
}

protected double computePrefWidth(double width) {
    return 1400;
}

protected double computePrefHeight(double height) {
    return 900;
}
}

HTML and JS code:

/*global angular*/
angular.module('app', []).controller('myCtrl', function($http, $scope) {
  $scope.criarLinks = function () {
      var responsePromise = $http.get("http://localhost:8182/consultas/");
      responsePromise.success(function (data, status, headers, config) {
        document.write("SUCESSO");
      });
      responsePromise.error(function (data, status, headers, config) {
        document.write("ERRO");
      });
    }
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><divng-app="app" ng-controller="myCtrl">
  <button ng-click="criarLinks()">Aperte!</button>
</div>

Restlet Code:

public class Server {
public static Component component = new Component();

public static void begin(String args[]) throws Exception {
    component.getServers().add(Protocol.HTTP, 8182);
    component.getDefaultHost().attach("/consultas", new ServerApp());
    component.start();
}
}

public class ServerApp extends Application {
private Router router = new Router(getContext());

public synchronized Restlet createInboundRoot() {
    router.attach("/", ServerAppResource.class);
    return router;
}

}

public class ServerAppResource extends ServerResource {

@Get
public Response returnConsultas() throws Exception {
    Response response = getResponse();
    response.setStatus(Status.SUCCESS_OK);
    return response;
}

}

Class main

public class AppLauncher
{
public static void main(String args[]) throws Exception {
    Server.begin(args);
    View.begin(args);
}
}

What happens is, even though it receives a success message, the Angular still executes the promise error method. In the IDE it works, but in JAR, it always goes to the error message.

    
asked by anonymous 11.12.2014 / 12:23

1 answer

3

Just add a header in the reply.

public class ServerAppResource extends ServerResource {

@Get
public Response returnConsultas() throws Exception {
  Response response = getResponse();
  Series<Header> responseHeaders = (Series<Header>) response.getAttributes().get("org.restlet.http.headers");
        if (responseHeaders == null) {
            responseHeaders = new Series<Header>(Header.class);
            response.getAttributes().put("org.restlet.http.headers", responseHeaders);
        }
  responseHeaders.add(new Header("Access-Control-Allow-Origin","*"));
  //Maldito header ^
  response.setStatus(Status.SUCCESS_OK);
  return response;
}
}
    
11.12.2014 / 17:37