javascript - Prevent preflight OPTIONS when using sub domains -
given 2 sub domains:
web.mysite.com
, api.mysite.com
currently making request web.
api.
results in preflight options request being made. wouldn't of issue if didn't add 600ms requests in china.
i told setting document.domain = 'mysite.com';
in js resolve issue hasn't helped @ all.
is possible / how can disable options request when sending different sub domain.
solved using iframe
technique seems facebook / twitter do.
steps below:
1) set document.domain
root domain. given url http://site.mysite.com/
set domain in javascript document.domain = 'mysite.com';
2) setup iframe pulls html file api domain.
<iframe id="receiver" src="http://api.mysite.com/receiver" style="position:absolute;left:-9999px"></iframe>
this set positioned can't seen.
3) set html of receiver page set domain:
<!doctype html><body><script>document.domain='mysite.net'</script></body></html>
4) added onload
event iframe capture window once loaded.
onload="window.tempiframecallback()"
5) assign child window variable.
window.tempiframecallback = function() { window.childwindow = window.receiver.contentwindow; }
6) make xmlhttprequest()
childwindow instead of main window.
var xhr = new window.childwindow.xmlhttprequest();
now requests sent without preflight options
request.
7) when using jquery, can set source of xhr in settings:
$.ajax({ ... xhr: function() { return new window.childwindow.xmlhttprequest(); } });
Comments
Post a Comment