简体   繁体   English

如何使用Java脚本函数从使用SWT浏览器从Ajax请求获取数据的Eclipse函数获取返回值?

[英]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 我在Eclipse中有此功能:我在Java中有一个按钮,可在javascript中触发此功能

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

Then I have this function in javascript 然后我在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. 因为ajax请求需要几秒钟的时间,并且我的javascript函数返回了一个空对象insted,等待请求返回数据。

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. 处理Ajax调用时,必须在获得结果后从Javascript代码中调用所谓的BrowserFuntion

Here's an example of how to define a BrowserFunction and how to call it from Javascript: 这是一个如何定义BrowserFunction以及如何从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: 此外,这里还有来自Vogella的有关Browser的出色教程:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从作为模块导出的请求中获取回调函数的返回值 - How to get the return value of the callback function from a request which is exported as a module 如何将Java脚本函数的返回值获取到在keyup上调用的变量 - How to get the return value of a java-script function to a variable which is called on keyup 从ajax请求返回值到另一个函数 - return value from ajax request to another function 如何从存根 function 获取返回值? - How to get return value from stub function? jsTree ajax-从调用的Web服务函数获取返回值 - jsTree ajax - Get the return value from called web service function 如何使用java脚本从函数内部获取值? - how can i get a value from inside a function using java script? 返回 https 从异步 function 获取请求正文值 - Return https get request body value from async function 如何从 Ajax 请求 function 返回 JSON? - How to return JSON from Ajax request function? 如何从包含来自另一个应用程序的数据库查询的 Node.js 函数获取返回值 - How to get the return value from Node.js function which contains DB query from another application 如何检测GET请求是来自AJAX请求还是浏览器请求? - How to detect if GET request comes from an AJAX request or browser request?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM