Ajax call fails with Javafx Webview

▼魔方 西西 提交于 2020-02-02 10:14:26

问题


Followed this, though Ajax calls from link executes without any errors, having below function in a HTML fails still

$(document).ready(function() {
       alert("Ready");
       $.ajax({
           url: "https://www.google.com/",
           type: 'GET',
           cache: false,
           data: "{'name':'hi'}",
           contentType: 'application/json',
           dataType: 'json',
           async: true,
           success: function(res) {
             alert('success res-:' + JSON.stringify(res));
           },
           error: function(res) {
             alert('error res-:' + JSON.stringify(res));
           }
        });

Update Java Code

   import javafx.application.Application;
   import javafx.scene.Scene;
   import javafx.scene.control.ButtonType;
   import javafx.scene.control.Dialog;
   import javafx.scene.web.WebView;
   import javafx.stage.Stage;
   import netscape.javascript.JSObject;

   public class FXWebViewSO extends Application  {

    public static void main(String[] args) {
    launch(args);
    }

    public void start(Stage primaryStage) {
    primaryStage.setTitle("JavaFX WebView Example");

    WebView webView = new WebView();
    webView.getEngine().setJavaScriptEnabled(true);
    webView.getEngine().setOnAlert(event -> showAlert(event.getData()));
    webView.getEngine().setJavaScriptEnabled(true);
    JSObject window = (JSObject) webView.getEngine().executeScript("window");
    window.setMember("window", null);

//  webView.getEngine().load("file://webview.html"); // Fails - HTML having above Ajax function
    webView.getEngine().load("http://www.jquerysample.com/#BasicAJAX"); // Works

    final Scene scene = new Scene(webView);
    primaryStage.setScene(scene);
    primaryStage.show();
    }

    private void showAlert(String message) {
    System.out.println(message);
    Dialog<Void> alert = new Dialog<>();
    alert.getDialogPane().setContentText(message);
    alert.getDialogPane().getButtonTypes().add(ButtonType.OK);
    alert.showAndWait();
    }
   }

Note: The same works fine in couple of windows machines and failes from Ubuntu Desktop.

Environment details below

Ubuntu - 1.8.0_151 - Failed (Tried upgrading too still no luck).

Windows7 - 1.8.0_221 - Works

来源:https://stackoverflow.com/questions/59926299/ajax-call-fails-with-javafx-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!