Javafx webview does not support Ajax web features

I am trying to open a webpage in webview using JavaFx. Its opening the webpage is correct, but it does not support Ajax-based web features such as partial update and popup popup. I use the following code

final Group group= new Group(); Scene scene= new Scene(group); fxpanel.setScene(scene); WebView webview = new WebView (); group.getChildren().add(webview); eng= webview.getEngine(); eng.setJavaScriptEnabled(true); try{ String url="http://www.abc.com"; eng.load(url); eng.setCreatePopupHandler( new Callback<PopupFeatures, WebEngine>() { @Override public WebEngine call(PopupFeatures config) { smallView = new WebView(); smallView.setFontScale(0.8); ChatPopup frm = new ChatPopup(smallView); frm.setBounds(0,0,400,250); frm.setVisible(true); return smallView.getEngine(); } }); } catch(Exception ex){} } 
+4
source share
2 answers

WebView supports Ajax.

  • Launch the following application.
  • Click the "Upload data from server to div" button.
  • The page will be refreshed with data received from the server.

ajaxwebview

 import javafx.application.Application; import static javafx.application.Application.launch; import javafx.scene.Scene; import javafx.scene.web.WebView; import javafx.stage.Stage; public class WebViewAjax extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { WebView webView = new WebView(); webView.getEngine().load("http://www.jquerysample.com/#BasicAJAX"); final Scene scene = new Scene(webView); stage.setScene(scene); stage.show(); } } 
+2
source

If you need to make AJAX calls for cross-site services in WebView, you can circumvent security restrictions by making your AJAX calls redirected to Java. For example, you could write or find a class with the ".request ()" method that takes JSObject as a parameter (the same JSObject format, which preferably uses the jQuery $ .ajax () method) and inserts a Java object that will expose this method:

 WebView myWebView; //assuming it initialized and points to an actual WebView WebEngine engine = myWebView.getEngine(); JSObject window = null; try{ window = (JSObject) engine.executeScript("window"); }catch (JSException e){ e.printStackTrace(); } if (window != null){ window.setMember("myAjax", new AJAXProxyClass()); } 

You can also directly override the jQuery ajax method with your own upcalling method, so the difference is completely transparent to javascript code, i.e.:

 engine.executeScript("$.ajax = new function (o) { myAjax.request(o); };"); engine.executeScript("_$ = window.$"); 

This will replace the jQuery "$ .ajax" call using one of your Java object without any problems. (I set the variable "_ $" because jQuery will overwrite $ with it sometimes if it detects conflicts, returning jQuery to its original version.) This means that in most cases, any javascript code does not have to worry about working whether it is in your webview or not.

A warning, however, is that the jQuery ajax call is quite complicated, and this may break some jQuery ajax extensions if you are not careful about how you handle it. It should work fine for the most common types of GET and POST calls.

+1
source

Source: https://habr.com/ru/post/1479294/


All Articles