How to get return value to eclipse function from java script function which get data from ajax request using SWT browser?

假如想象 提交于 2019-12-25 09:25:53

问题


I have tried below....

I have this in eclipse : I have a button in java which triggers this function in javascript

    Object status =  browserCtrl.evaluate("return atm.java.webToJavaPerspective()");

Then I have this function in javascript

function atm.java.webToJavaPerspective(){
  returnData = {};
  //ajaxRequest = some ajaxRequest variable 
  $.when(ajaxRequest).then(function( data, textStatus, jqXHR ) {
    //modify the data
    returnData.textStatus = textStatus;  

    //this return statement should return data to java function
    return returnData;
  });

//this will return empty object
return returnData;
}

But I am always getting empty object. Because the ajax request takes few seconds, and my javascript function returns the empty object insted waiting for request to return data.

How can I achieve this..?


回答1:


When dealing with Ajax calls, you'll have to call a so-called BrowserFuntion from your Javascript code when you have the result.

Here's an example of how to define a BrowserFunction and how to call it from Javascript:

public static void main(String[] args)
{
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    Browser browser = new Browser(shell, SWT.NONE);
    browser.setText("<a href='#' onClick='theJavaFunction()'>Click me!</a>");

    new BrowserFunction(browser, "theJavaFunction")
    {
        @Override
        public Object function(Object[] objects)
        {
            System.out.println("Call from Javascript");

            return null;
        }
    };

    shell.pack();
    shell.open();

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
        {
            display.sleep();
        }
    }
}

Furthermore, an excellent tutorial about Browser from Vogella here:

http://blog.vogella.com/2009/12/21/javascript-swt/



来源:https://stackoverflow.com/questions/44218165/how-to-get-return-value-to-eclipse-function-from-java-script-function-which-get

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