从 vaadin 客户端向外部服务器发出请求并 return JSON 响应我的服务器
Make request to external server from vaadin client and return JSON response to my sever
使用 api 为我提供了一个仅适用于客户端 ip 的访问令牌,因此我正在尝试向客户端和 return 上的外部站点发出请求 JSON 响应我的服务器。问题是如何发出请求并将 JSON 存储在客户端,以便我可以将其发送到服务器。
谢谢
看看 Vaadin 的
Integrating JavaScript Components and Extensions
此处:
https://vaadin.com/docs/-/part/framework/gwt/gwt-javascript.html#gwt.javascript.rpc
您可以创建一个 JavaScript 连接器组件,然后您可以使用它来制作 RPC,如下所示:
@JavaScript({"mycomponent-connector.js"})
public class MyComponent extends AbstractJavaScriptComponent {
public MyComponent(){
// when you create the component
// add a function that can be called from the JavaScript
addFunction("returnResponse", new JavaScriptFunction() {
@Override
public void call(JsonArray arguments) {
String response = arguments.getString(0));
// do whatever
}
});
}
// set up a way to make the request
public void makeRequest(String url) {
callFunction("makeRequest", url);
}
}
使用 JavaScript 文件 mycomponent-connector.js
(使用 XMLHttpRequest
示例):
window.com_example_mypackage_MyComponent =
function() {
var connector = this;
// add a method to the connector
this.makeRequest = function(theUrl){
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
connector.returnResponse(xmlHttp.responseText);
}
};
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.send(null);
}
};
在服务器端调用方法 MyComponent.makeRequest("myurl")
将触发客户端的 makeRequest
方法。当返回响应时,我们调用 connector.returnResponse(xmlHttp.responseText)
将其发送回服务器并由 MyComponent
.
的构造函数中添加的 "returnResponse"
函数处理
使用 api 为我提供了一个仅适用于客户端 ip 的访问令牌,因此我正在尝试向客户端和 return 上的外部站点发出请求 JSON 响应我的服务器。问题是如何发出请求并将 JSON 存储在客户端,以便我可以将其发送到服务器。
谢谢
看看 Vaadin 的
Integrating JavaScript Components and Extensions
此处:
https://vaadin.com/docs/-/part/framework/gwt/gwt-javascript.html#gwt.javascript.rpc
您可以创建一个 JavaScript 连接器组件,然后您可以使用它来制作 RPC,如下所示:
@JavaScript({"mycomponent-connector.js"})
public class MyComponent extends AbstractJavaScriptComponent {
public MyComponent(){
// when you create the component
// add a function that can be called from the JavaScript
addFunction("returnResponse", new JavaScriptFunction() {
@Override
public void call(JsonArray arguments) {
String response = arguments.getString(0));
// do whatever
}
});
}
// set up a way to make the request
public void makeRequest(String url) {
callFunction("makeRequest", url);
}
}
使用 JavaScript 文件 mycomponent-connector.js
(使用 XMLHttpRequest
示例):
window.com_example_mypackage_MyComponent =
function() {
var connector = this;
// add a method to the connector
this.makeRequest = function(theUrl){
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
connector.returnResponse(xmlHttp.responseText);
}
};
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.send(null);
}
};
在服务器端调用方法 MyComponent.makeRequest("myurl")
将触发客户端的 makeRequest
方法。当返回响应时,我们调用 connector.returnResponse(xmlHttp.responseText)
将其发送回服务器并由 MyComponent
.
"returnResponse"
函数处理