将参数从javascript传递到gwt?
我是Gwt的新手,我正在实施一个webclipper项目,所以我的任务是将一些参数从javascript文件发送到Gwt,这样我就能够与我的couchdb数据库建立连接,但我在传递诸如title、url、,从网页到Gwt n再到couchdb的摘要。以下代码是我的javascript代码:-将参数从javascript传递到gwt?,java,javascript,gwt,Java,Javascript,Gwt,我是Gwt的新手,我正在实施一个webclipper项目,所以我的任务是将一些参数从javascript文件发送到Gwt,这样我就能够与我的couchdb数据库建立连接,但我在传递诸如title、url、,从网页到Gwt n再到couchdb的摘要。以下代码是我的javascript代码:- function onPageInfo(o) { document.getElementById('title').value = o.title; document.
function onPageInfo(o) {
document.getElementById('title').value = o.title;
document.getElementById('url').value = o.url;
document.getElementById('summary').innerText = o.summary;
}
// Global reference to the status display SPAN
var statusDisplay = null;
// POST the data to the server using XMLHttpRequest
function addBookmark() {
// Cancel the form submit
event.preventDefault();
// The URL to POST our data to
var postUrl = "http://127.0.0.1:8888/practice.html? gwt.codesvr=127.0.0.1:9997&gwt.codesvr=127.0.0.1:9997/?title=1&url=2&summary=3";
// Set up an asynchronous AJAX POST request
var xhr = new XMLHttpRequest();
xhr.open('POST', postUrl, true);
// Prepare the data to be POSTed
var title = encodeURIComponent(document.getElementById('title').value);
var url = encodeURIComponent(document.getElementById('url').value);
var summary = encodeURIComponent(document.getElementById('summary').value);
var tags = encodeURIComponent(document.getElementById('tags').value);
var params = 'title=' + title +
'&url=' + url +
'&summary=' + summary +
'&tags=' + tags;
// Replace any instances of the URLEncoded space char with +
params = params.replace(/%20/g, '+');
// Set correct header for form data
xhr.setRequestHeader('Content-type', 'application/json');
// Handle request state change events
xhr.onreadystatechange = function() {
// If the request completed
if (xhr.readyState == 4) {
statusDisplay.innerHTML = '';
if (xhr.status == 200) {
// If it was a success, close the popup after a short delay
statusDisplay.innerHTML = 'Saved!';
window.setTimeout(window.close, 1000);
} else {// Show what went wrong
statusDisplay.innerHTML = 'Error saving: ' + xhr.statusText;
}
}
};
// Send the request and set status
xhr.send(params);
statusDisplay.innerHTML = 'Saving...';
}
// When the popup HTML has loaded
window.addEventListener('load', function(evt) {
// Handle the bookmark form submit event with our addBookmark function
document.getElementById('addbookmark').addEventListener('submit', addBookmark);
// Cache a reference to the status display SPAN
statusDisplay = document.getElementById('status-display');
// Call the getPageInfo function in the background page, injecting content_script.js
// into the current HTML page and passing in our onPageInfo function as the callback
chrome.extension.getBackgroundPage().getPageInfo(onPageInfo);
});
谢谢….您可以通过导出java文件(GWT客户端模块)中定义的函数来调用该函数。让我们假设有一个类a.java,它也是您的入口点类。这个类包含someMethod(),您需要通过javascript调用它来传递一些参数。你A班的内容大概是
public class A implements EntryPoint {
public static functionExported = false;
public void onModuleLoad() {
ExportToBeCalledFromJs();
// other code goes here
}
public static native void ExportToBeCalledFromJs() /*-{
$wnd.toBeCalledFromJs = $entry(function(s1, s2) {
return @com.practice.gwt.client.A::someFunction();
});
@com.practice.gwt.client.A:functionExported = true;
}-*/;
}
上面的代码导出函数并使其可用于javascript。您只需从js调用toBeCalledFromJs(param1,param2),其中param1将替换s1,param2将替换s2。如果您希望添加更多参数,您可以修改上面代码中的$entry(函数(s1,s2)。先生,这个函数导出的是什么?sry,我只是键入了代码,没有执行。它是一个布尔值。函数导出后,它的值更改为true