Javafx Webview does not receive 200 messages from AJAX

Asked

Viewed 181 times

0

I am 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 page 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>
<div ng-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;
}

}

Main class

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

What happens is, even when you receive a success message, Angular still executes the error method of the Promise. In the IDE it works, but in the JAR, it always goes to the error message.

1 answer

3


Just adding a header to 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;
}
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.