One jQuery instance, two domains

I have two pages: a.example.com and b.example.com

a.example.com includes jQuery

a.example.com contains an iframe pointing to b.example.com

both pages have document.domain set to the same parent domain, example.com

How can I use jQuery include from a.example.com to call $ .ajax ({url: " b.example.com "}) from within b.example.com iframe?

In other words: both pages can currently access Javascript from each other, but I can't get the AJAX call to function without throwing an XSS error. That is, without including jQuery on b.example.com too. How do I avoid including jQuery twice?

Example iframe content:

<script> document.domain = "example.com"; function proxyAjax() { var jQueryParent = parent.$.sub(); // Chrome gives error: XMLHttpRequest cannot load http://b.example.com/. Origin http://a.example.com/ is not allowed by Access-Control-Allow-Origin. jQueryParent.ajax({ url : "http://b.example.com/", success : function() { console.debug("Success"); } }); } proxyAjax(); </script> 
+4
source share
1 answer

Setting document.domain will not allow you to make cross-domain AJAX calls. If you really don't need a create request with b.example.com, you can do this:

In a.example.com document, use this JavaScript function:

 function makeAjaxRequest(callback) { $.ajax({ url: '...', success: callback }) } 

Then, in b.example.com, find the script that calls makeAjaxRequest in a.example.com with the callback function defined in b.example.com.

+1
source

Source: https://habr.com/ru/post/1390330/


All Articles