Carlos Bribiescas
Carlos Bribiescas

Reputation: 4427

Get any page with AJAX

I'm new to AJAX and I have what I think is a simple question. I know you can create a page that will respond to an AJAX call. Is it possible to just get any page with an AJAX call?

So I mean to say, can I do anything with an AJAX call that I could do with a URL?

EDIT #1

Thanks for all the responses! Really helped clarify!

Upvotes: 3

Views: 68

Answers (3)

yoozer8
yoozer8

Reputation: 7489

No.

One example is that you can't use AJAX to upload or download files. One workaround for this is to target the upload or download to a hidden iframe and poll that frame for a response. Update: it seems some support for this is part of HTML 5 (see https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications).

A second example is navigating the user to another page. You can load a second page and replace the contents of the window with it, but the URL will still be the original page (the "refresh" and "back" features of the browser will behave accordingly).

A third is cross-domain requests. AJAX calls are limited to the domain the page originated from.

Upvotes: -1

Mike Brant
Mike Brant

Reputation: 71422

Yes and no.

AJAX is a powerful mechanism by which you can retrieve and/or load data into the DOM in a flexible manner. You can do things like grab the content of another page and display all or portions of it on your page. There is a catch however.

Due to security reasons, you cannot depend on being able to make an AJAX call in a cross-domain manner unless the server on the other domain is properly configured. This is known as Cross-Origin Resource Sharing (CORS). You can read more about that here - http://en.wikipedia.org/wiki/Cross-origin_resource_sharing

Alternatively, some servers will expose API's that work with JSONP (JSON with Padding), which is a sort of workaround for the Same Origin Policy (SOP) that normally prevents cross-domain requests of this nature. In JSONP, the remote endpoint in essence wraps the response in a javascript function. You can read more about JSONP here - http://en.wikipedia.org/wiki/JSONP

Upvotes: 4

Matt Pileggi
Matt Pileggi

Reputation: 7196

You are limited to requests within the same domain, unlike a normal URL. There are ways around it using CORS or JSONP in that case.

http://en.wikipedia.org/wiki/Cross-origin_resource_sharing

Upvotes: 3

Related Questions