无法将GWT连接到BigCommerce API
标准:我正在尝试使用GWT RequestBuilder连接到名为BigCommerce的安全web服务API。 这是我的切入点:无法将GWT连接到BigCommerce API,gwt,cross-domain,bigcommerce,same-origin-policy,Gwt,Cross Domain,Bigcommerce,Same Origin Policy,标准:我正在尝试使用GWT RequestBuilder连接到名为BigCommerce的安全web服务API。 这是我的切入点: public class GwtTest implements EntryPoint { String url = "http://my-url-api/api/v2/products.xml"; // not the original url i'm using @Override public void onModuleLoad() {
public class GwtTest implements EntryPoint {
String url = "http://my-url-api/api/v2/products.xml"; // not the original url i'm using
@Override
public void onModuleLoad() {
url = URL.encode(url);
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
builder.setHeader("Authorization", "Basic XXX"); // I generated this from Postman app on Chrome where things work perfectly
builder.setHeader("Access-Control-Allow-Credentials", "true");
builder.setHeader("Access-Control-Allow-Origin", "http://127.0.0.1:8888/");
builder.setHeader("Access-Control-Allow-Methods", "POST, GET, UPDATE, OPTIONS");
builder.setHeader("Access-Control-Allow-Headers", "x-http-method-override");
builder.setHeader("Content-Type", "application/xml");
try {
builder.sendRequest(url, new RequestCallback() {
@Override
public void onResponseReceived(Request request, Response response) {
RootPanel.get().add(new HTML("Success: "+response.getText()));
}
@Override
public void onError(Request request, Throwable exception) {
RootPanel.get().add(new HTML("Failure (Response Error): "+exception));
}
});
} catch (RequestException e) {
RootPanel.get().add(new HTML("Failure Request Exception: "+e));
}
}
}
遇到错误:我首先遇到同一来源地策略错误:
然后,在浏览器上禁用CORS后,出现Perflight错误:
解决方法:我通过在Chrome上禁用网络安全功能获得了结果,但我认为这不是正确的解决方案
小提示:请指导我,伙计们,因为我是GWT和BigCommerce的新手,谢谢。你可以在你的webapp中包含一个servlet,作为客户端和BigCommerce之间的代理 另一种方法是运行一个反向代理(如Apache httpd),使对BigCommerce服务器的请求看起来与您的webapp位于同一主机上 下面是一个Apache httpd配置文件的示例,该文件将作为Web应用程序和其他主机上的外部服务的反向代理。在浏览器中,webapp和外部服务似乎都在同一台主机上运行,这正是您想要的
# file: /etc/httpd/conf.d/mywebapp.conf
<VirtualHost *:80>
ProxyPreserveHost On
# Forward requests to path /SomeExternalService to an external service
ProxyPass /SomeExternalService http://externalhost/
# Forward all other requests to a local webserver hosting your webapp,
# such as Tomcat listening on port 8081
ProxyPass / http://127.0.0.1:8081/
ProxyPassReverse / http://127.0.0.1:8081/
</VirtualHost>
#文件:/etc/httpd/conf.d/mywebapp.conf
代理主机
#将对path/SomeExternalService的请求转发到外部服务
ProxyPass/SomeExternalServicehttp://externalhost/
#将所有其他请求转发到承载您的webapp的本地Web服务器,
#例如Tomcat在端口8081上侦听
ProxyPass/http://127.0.0.1:8081/
ProxyPassReverse/http://127.0.0.1:8081/
您可以在Web应用程序中包含一个servlet,作为客户端和BigCommerce之间的代理
另一种方法是运行一个反向代理(如Apache httpd),使对BigCommerce服务器的请求看起来与您的webapp位于同一主机上
下面是一个Apache httpd配置文件的示例,该文件将作为Web应用程序和其他主机上的外部服务的反向代理。在浏览器中,webapp和外部服务似乎都在同一台主机上运行,这正是您想要的
# file: /etc/httpd/conf.d/mywebapp.conf
<VirtualHost *:80>
ProxyPreserveHost On
# Forward requests to path /SomeExternalService to an external service
ProxyPass /SomeExternalService http://externalhost/
# Forward all other requests to a local webserver hosting your webapp,
# such as Tomcat listening on port 8081
ProxyPass / http://127.0.0.1:8081/
ProxyPassReverse / http://127.0.0.1:8081/
</VirtualHost>
#文件:/etc/httpd/conf.d/mywebapp.conf
代理主机
#将对path/SomeExternalService的请求转发到外部服务
ProxyPass/SomeExternalServicehttp://externalhost/
#将所有其他请求转发到承载您的webapp的本地Web服务器,
#例如Tomcat在端口8081上侦听
ProxyPass/http://127.0.0.1:8081/
ProxyPassReverse/http://127.0.0.1:8081/
使用代理servlet
这也是GWT Openlayers包装器使用的解决方案
使用代理servlet
这也是GWT Openlayers包装器使用的解决方案
根据Rob Newton的回答,如果您的应用程序是纯前端,您可以将文件托管在nginx上,并向配置中添加一些代理传递指令,例如:
location ~* ^/bigcommerce/(.*) {
proxy_pass http://api.bigcommerce.com/$1$is_args$args;
}
因此,无论何时调用
http://hostaddress/bigcommerce/something
,这将被禁止http://api.bigcommerce.com/something
。此配置中不支持标题,您可以为此添加更多指令 根据Rob Newton的回答,如果您的应用程序是纯前端,您可以将文件托管在nginx上,并在配置中添加一些proxy_-pass指令,例如:
location ~* ^/bigcommerce/(.*) {
proxy_pass http://api.bigcommerce.com/$1$is_args$args;
}
因此,无论何时调用
http://hostaddress/bigcommerce/something
,这将被禁止http://api.bigcommerce.com/something
。此配置中不支持标题,您可以为此添加更多指令 这似乎是一个与GWT无关的问题,但更多的是关于他们的API是如何设计的。我认为您的解决方案是将GWT应用程序连接到将调用API的后端。要了解更多信息,您可以在这里查看答案:您也可以使用ngrok来运行它。建议在现场的工具部分使用。这似乎是一个与GWT无关的问题,但更多的是关于他们的API是如何设计的。我认为您的解决方案是将GWT应用程序连接到将调用API的后端。要了解更多信息,您可以在这里查看答案:您也可以使用ngrok来运行它。建议在现场的工具部分使用。你能给我举个例子吗?我如何在我的项目上运行这个servlet?我对这个完全陌生,但我必须让它工作。我如何在我的项目上运行这个servlet?我是个新手,但我必须让它发挥作用