jpillora / xdomain

A pure JavaScript CORS alternative
https://jpillora.com/xdomain/
3.12k stars 270 forks source link

Is there a way to scope the requests handled by xdomain? #109

Closed kswin closed 10 years ago

kswin commented 10 years ago

Hello,

When loading xdomain, it looks like all xhrs are affected. I'm interested in targeting specific api calls with xdomain. I apologize if this isn't the correct place to post these questions.

Thanks

jpillora commented 10 years ago

Yes this is the right place for questions and yes xdomain hooks all xhr, if the use case is you'd like some requests to use CORS, then the answer is: just don't add a slave for those domains, or if it's the same domain, just white list the desired path

On Saturday, September 6, 2014, kswin notifications@github.com wrote:

Hello,

When loading xdomain, it looks like all xhrs are affected. I'm interested in targeting specific api calls with xdomain. I apologize if this isn't the correct place to post these questions.

Thanks

— Reply to this email directly or view it on GitHub https://github.com/jpillora/xdomain/issues/109.

kswin commented 10 years ago

so, if I only want the slave to process requests going to "my-api-target", then on the slave, I would have something like:

Not sure if I got that right... i really appreciate the help!

jpillora commented 10 years ago

Yep, given my-api-target is a domain

On Saturday, September 6, 2014, kswin notifications@github.com wrote:

so, if I only want the slave to process requests going to "my-api-target", then on the slave, I would have something like:

Not sure if I got that right... i really appreciate the help!

— Reply to this email directly or view it on GitHub https://github.com/jpillora/xdomain/issues/109#issuecomment-54639072.

kswin commented 10 years ago

Actually, I think i need to elaborate my use-case. I changed the master attribute and it wasn't the outcome that I expected.

I have an app that makes xhr requests to the same domain and a different api domain. On my app, I have: slaves = {}; slaves[apiDomain] = '/proxy.html'; xdomain.slaves(slaves);

From the api domain, I serve proxy.html with:

This caused all xhr calls from my app to go through xdomain, including a $.ajax call to the app domain with async=false. xdomain doesn't support sync requests so it caused an error in the app. This illustrated that we wanted to scope xdomain to requests that matched the apiDomain. I tried to change data-master from * to the apiDomain, but now the cross domain call doesn't work anymore. The Networks tab on dev tools does not display any calls to apiDomain and the console reads "xdomain (apiDomain): blocked request from: 'app domain'. Also, I still see the error caused by $.ajax async=false.

I would like calls going to the apiDomain to be processed by xdomain, but only those. Does the additional background give any ideas on what I'm doing wrong? Or maybe it's inherent in the nature of xdomain that all xhr requests will be processed by it?

kswin commented 10 years ago

Well, I just found #60.. which is basically my issue..

Also, re-read the FAQs more carefully, and I see that "XDomain attempts to perfectly implement XMLHttpRequest2."

So I guess all my questions are answered - thanks!