/    Sign up×
Community /Pin to ProfileBookmark

Ajax from a different domain

Hi,

I have an ajax script that I would like to use to retrieve a password hint from a database.

Both the ajax script and the php retrieval script are stored on the same domain (we’ll call it domain1.com), but the ajax script runs on another domain (domain2.com).

All the scripts are working fine, individually, but when I try to integrate them, I’m getting a ready state of 4, but an HTTP status of 0. I’ve traced the code flow and the retrieval script is being called by the ajax script OK and it’s echoing all the right stuff, but it’s just not getting back to calling ajax routine.

I’ve read that an ajax routine can only call a script that resides on the same domain that it runs on. Is this correct and, if so, does anyone know of a work around, because I really need to be able to store the ajax script on domain1 and run it on domain2?

If not, then can anyone suggest why I’m getting an HTTP status of 0, when all the individual components are working fine?

Debbie

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@mityaNov 16.2009 — "I've read that an ajax routine can only call a script that resides on the same domain that it runs on. "[/QUOTE]

Correct. It's part of the same origin policy.

The workaround is you host the PHP on the same domain as your ajax script and have it connect to the other domain's database remotely. Provided the server allows it, PHP can talk to any database on any host, as long as you have the IP and login details.

If for any reason you can't do this and the DB-accessing script MUST live on the other domain, then you need a cURL (www.php.net/curl) script on domain1 to talk to the other domain.
Copy linkTweet thisAlerts:
@slaughtersNov 16.2009 — Ajax cross domain scripting is a security issue and is not allowed by the browsers

BUT

If you just need to display the output of a server side select statement then you can use an iframe to retrieve and display the remote server information (just reassign the iframes .src attribute).

This is a useful work around for tooltips, reports, and other info only windows, but not at all useful if you need to do anything other than display it to the screen.

The only other way I know of to grab information off of a remote server is via XML, i.e. RSS feeds, or through page scraping.
Copy linkTweet thisAlerts:
@rnd_meNov 16.2009 — can you modify the config of domain2's servers?

if so, set the access-control headers, to enable 3rd party ajax in supported browsers.

for even more widespread compatibility, add a crossdomain.xml that allows cross-domain flash access, which you can then use to "ajax" files from one site to another.

finally, if you can't modify the config, but can add a server page, consider coding a simple JSONP interface that allows any client to fetch domain2's content via a script tag.
Copy linkTweet thisAlerts:
@slaughtersNov 16.2009 — JSONP. I can't believe I've not used that before. I blame it on the fact that most of my server side coding tends to be in the same domain and uses XML more often than not.

So - for the original poster - here is a good explanation of JSONP:

What in the heck is JSONP and why would you use it?

http://www.insideria.com/2009/03/what-in-the-heck-is-jsonp-and.html
Copy linkTweet thisAlerts:
@rnd_meNov 17.2009 — i should mention that yql will let you turn any page into jsonp for free without end-user registration...
×

Success!

Help @DebbieFigg spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...