2016-04-14 5 views
1

クライアントIPでのみ動作するアクセストークンを与えるapiを使用しているので、クライアント上のその外部サイトに要求を出そうとしています私のサーバーにJSON応答を返します。問題はリクエストを作成してJSONをクライアントに保存する方法です。そうすればJSONをサーバーに送ることができます。vaadinクライアントから外部サーバーに要求を行い、サーバーにJSON応答を返す

答えて

3

がここにはJavaScriptコンポーネントと拡張機能

を統合Vaadinの

を見ていただきありがとうございます:

https://vaadin.com/docs/-/part/framework/gwt/gwt-javascript.html#gwt.javascript.rpc

あなたがJavaScriptの接続を作成することができますまたはあなたは、その後のRPCのようにようにするために使用できるコンポーネント:JavaScriptを使用して

@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); 
    } 

} 

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); 
    } 
}; 

makeRequestを発射するサーバー側のメソッドMyComponent.makeRequest("myurl")を呼び出しますメソッドを呼び出します。応答が返されたときにはconnector.returnResponse(xmlHttp.responseText)を呼び出してこれをサーバに返信し、"returnResponse"の関数をMyComponentのコンストラクタに追加して処理します。

+0

= I'mは愚かかもしれないが、人々はその詳細な説明時に、私は好き) はあなたの時間に感謝、ありがとうございます。 –

関連する問題