Reputation: 71
I am creating this client webapp without server side. For example, I want this client to simply make HTTP request with public websites, like http://www.google.com. So far I have used RequestBuilder to build my HTTP requests, but when I run the app in dev mode, the response code for my request is always 0. And after I built it and made request on hmtl file, it gave the same result. It looks to me that the client is not communicating with the public internet but only local server (which is empty).
String url = "http://www.google.com";
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(url));
Is there anyway that I can make this work? Thanks!
Upvotes: 0
Views: 98
Reputation: 9741
You cannot make XHR requests to a different server from where you download your page, unless you enable CORS in the server.
google.com has not enabled CORS, so you cannot request their pages usin XHR.
If you own the domain you want to request enable CORS, here you have a filter example for a java backend.
If you simply want to display a 3party web in your ui you can insert it as an iframe, but be aware that you cannot read the iframe content because it is in a different domain.
Note that CORS works in almost browsers, but for IE/8-9 you have to use XDomainRequest
Upvotes: 1
Reputation: 713
You are running into the Same Origin Policy restriction. Basically, it's impossible to make requests to a different domain directly. If you just want to display that website as is, you can use an iframe to do it; if you want to make more specific requests, then you will need to use something like JSONP to do it, but that requires that the server actually support it.
Upvotes: 0