views:

105

answers:

2

i need to fetch a url with javascript/jquery and not php.

i've read that you could do that if you got a php proxy, but that means that it is still going through php. cause then it's still the ip of the server that is fetching it.

could one fetch the url entirely with only front-end, and thus fetch it with the client's ip?

+1  A: 

The problem is that jQuery would fetch an url with AJAX and AJAX won't operate cross-domain because of the potential security (as per the same-origin policy).

There are however ways to emulate this, if you load the page in an iframe you can retrieve the data by using innerHTML on the iframe. Here's an example script that uses jQuery: http://code.google.com/p/jquery-crossframe/

WoLpH
Modern browsers won't allow you to get the `innerHTML` of a cross-domain frame.
Max Shawabkeh
You're right, you can only communicate with the iframe via fragments. I was under the impression this was still possible.
WoLpH
+5  A: 

There exists a Same origin policy for AJAX requests. This prevents Javascript on, say, this site, from making a request to gmail.com (with your cookies), reading your e-mails, and uploading them to the StackOverflow server. Javascript on stackoverflow.com can only make AJAX requests to pages on that domain.

As you can see, this is essential for security. Requests must instead be made by a proxy running on your web server - PHP can be used, but there are other solutions. For example, Ajax Cross Domain is an AJAX library that communicates with a Perl script running on the server to emulate AJAX requests for other domains.

It is also possible to make requests on other domains via a javascript include (script tag), image tag, etc. but in these cases you cannot read the contents of the page.

You cannot do this with an iframe either: scripts cannot see the internals of iframes unless they are on the same domain as the script.

So in short, use a proxy.

rjh
so that means that i HAVE TO fetch the page with the server's ip address? there is no way to get it with a client's ip, and then send the results back to my server? but doesn´t google maps api allow us to make requests from only javascript, thus getting results from the client´s ip? so why does that work then?
never_had_a_name
With Google Maps API, you load Javascript from google's server (no problems there - you can run the javascript, you just can't see the contents of the file). The tile images etc. are on Google's server but linking to external images is always allowed. For a few things (routing, geocoding) the API "talks" to Google by writing a <script> tag with a special request, and the returned javascript is immediately executed and e.g. sets some variables to some JSON data. That approach is clever, but only works if the other server is co-operating.
rjh
If you're curious about cross-domain JSON requests, see http://www.xml.com/pub/a/2005/12/21/json-dynamic-script-tag.html This doesn't apply for general web requests, though.
rjh