views:

66

answers:

4

Something I find really confusing, is why are ajax requests limited to the same domain? What is the reasoning behind this?

I don't see any problem with requesting files from external locations, also servers making XMLHTTP requests seem to get and post to external locations fine.

A: 

Here is some information to satisfy your question: http://en.wikipedia.org/wiki/Same_origin_policy

Shamanu4
Thanks for the link but it still doesn't give me a satisfactory reason WHY it's not permitted, it just describes it as a security concept.
Tom Gullen
+1  A: 

It's for security purposes - if a website could execute AJAX calls to any domain they wanted on the client side, it poses a serious risk.

There are ways around this though - you could have your AJAX call a PHP script on the same domain, which in turn can call a script from another domain and return it. This wouldn't be using the browser as the communication medium though, it'd be using your web server.

xil3
Thanks, I know it poses a security risk but no one has explained what this is yet.
Tom Gullen
+3  A: 

Picture this :

You come on my fabulous website www.halfnakedgirls.com. You have fun watching what looks like technical documentation on human physiology, but behind your back, some lines of javascript are executing some request another domain, let's say www.yourpaypallike.com.

Requests like http://www.yourpaypallike.com/account/[email protected]&amount=984654 or http://www.mymailprovider.com/mails/export?format=csv.

Do you know see why it is forbidden ? =)

Clement Herreman
Thanks, that makes a lot of sense. Assuming the user is logged in to Paypal, then the evil website could access an external site as if they were logged ino to it.
Tom Gullen
+1  A: 

Tom, It is not "Ajax request limited".. Ajax is based on Javascript. For security reason javascript is prohibited access on cross domains. If you really want to do cross domain Ajax, you can do a hack.

YourPage(Ajax) ----> YourServer ----> ExternalDomain

You can call a page in yourserver usin Ajax, Your domain will call to external domain using server side , and get the result then return to you as Ajax response.

Muneer