简体   繁体   中英

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

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..?

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM