问题
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