如何使用SWT浏览器从java脚本函数获取返回值给eclipse函数,而Java脚本函数从ajax请求获取数据? [英] 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....
我在Eclipse中有此功能:我在Java中有一个按钮,可在javascript中触发此功能 >
Object status = browserCtrl.evaluate("return atm.java.webToJavaPerspective()");
然后我在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;
}
但是我总是得到空对象。因为ajax请求需要几秒钟,而我的javascript函数会返回一个空对象,插入的是等待请求返回数据的对象。
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..?
推荐答案
在处理Ajax调用时,您必须调用所谓的 BrowserFuntion
从Javascript代码中获得结果。
When dealing with Ajax calls, you'll have to call a so-called BrowserFuntion
from your Javascript code when you have the result.
以下是如何定义 BrowserFunction
和如何从Javascript调用它:
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();
}
}
}
此外,关于来自Vogella的浏览器
:
Furthermore, an excellent tutorial about Browser
from Vogella here:
http://blog.vogella.com/2009/12/21/javascript-swt/
这篇关于如何使用SWT浏览器从java脚本函数获取返回值给eclipse函数,而Java脚本函数从ajax请求获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!