2012-03-03 2 views
1

GWT 애플리케이션을 사용하여 도메인 간 요청을 시도하는 동안 Chrome에서이 오류가 발생합니다.GWT를 통한 도메인 간 요청

Origin http://127.0.0.1:8888 is not allowed by Access-Control-Allow-Origin. 

나는 GET 요청을 보내기 위해 다음 코드를 시도했다.

import com.google.gwt.core.client.EntryPoint; 
import com.google.gwt.http.client.Request; 
import com.google.gwt.http.client.RequestBuilder; 
import com.google.gwt.http.client.RequestCallback; 
import com.google.gwt.http.client.RequestException; 
import com.google.gwt.http.client.Response; 

import com.google.gwt.user.client.ui.Label; 
import com.google.gwt.user.client.ui.RootPanel; 


public class Detracker implements EntryPoint { 
    public void onModuleLoad() { 
     doGet("http://www.google.com"); 
    } 

    public static void doGet(String url) { 
     RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url); 

     try { 
      builder.sendRequest(null, new RequestCallback() { 
       public void onError(Request request, Throwable exception) { 
        // Code omitted for clarity 
       } 

       @Override 
       public void onResponseReceived(Request request, 
         Response response) { 
        final Label msgLabel = new Label(); 
        msgLabel.setText(response.getText()); 
        RootPanel.get("resultContainer").add(msgLabel); 
       } 
      }); 

     } catch (RequestException e) { 
      // Code omitted for clarity 
     } 
    } 
} 
+0

는이 보안상의 제약 – dldnh

+0

에, 불행하게도, 허용하지 않는다 어떤 트릭/제가 알고하지 않는 것이이 –

+2

사용할 수 있습니다 해킹입니다. 우리는 GWT 앱의 백엔드에서 실행되고 프록시의 역할을하는 작은 서블릿 또는 JSP를 작성했습니다. 그것은 실제 Java를 실행하고 있으므로 원하는대로 GET/POST 매개 변수를 전달하여 응답을 얻고 GWT 클라이언트로 다시 보냅니다. 코드를 공유 할 수 없지만 내 고용주에게 속한 것은 유감입니다. – dldnh

답변

1

를 참조하십시오. :)

String message = ""; 


try { 
    URL url = new URL("working-url"); 
    URLConnection urlConn = url.openConnection(); 
    urlConn.setReadTimeout(100000); 
    BufferedReader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream())); 
    String line; 

    while ((line = reader.readLine()) != null) { 
     message = message.concat(line); 
    } 
    reader.close(); 

} catch (MalformedURLException e) { 
message = e.getMessage(); 
} catch (IOException e) { 
message = e.getMessage(); 
} 
2

크로스 도메인 요청에는 JSONP을 사용하십시오. (하지만 몇 가지 제한이 있습니다 - 메서드 만 가져올 수 있습니다)

또 다른 방법은 GWT의 서블릿을 사용하여 요청 결과를 얻은 다음 클라이언트에게 반환하는 것입니다. 또한 iframe과 함께 해킹이 존재하며 html5도 crossdomain 요청을 할 수 있습니다.

+0

GWT의 서블릿에서 같은 것을 시도했지만, 항상 유형에 대한 소스를 찾을 수 없음과 같은 예외가 발생합니다. 또한 GWTQuery를 시도했지만 올바르게 구현할 수 없습니다. 인터넷에서 사용할 수있는 작업 예제에 대해 알고 계십니까? 감사합니다. –