GoogleChromeのクロスドメイン制限について
GoogleChromeのクロスドメイン制限について。
2009/10/28 04:53 version3.0.195.27
2009/11/7
参照記事の読み間違いをしてました。サーバー側の.jsonに対する設定が必須です。したがって、XHR(XMLHttpRequest)でクロスドメインはできません><
ソースコード拝借元はこちら
http://d.hatena.ne.jp/os0x/20090610/1244618814
// 普通にXHRを実行。".json"ファイルはクロスドメイン制限に引っ掛からない。
javascript:(function(){ var xhr=window.XDomainRequest?new XDomainRequest:new XMLHttpRequest; try{ xhr.onload=function(){ alert([xhr.responseText,xhr]); }; xhr.open("GET","http://ss-o.net/json/wedataAutoPagerize.json"); xhr.send(); }catch(e){ alert(e.message); } })();
// ページ上に埋め込んでもOK。
javascript:(function(){ var scr=document.createElement('script'); scr.type='text/javascript'; scr.text='('+function(){ var xhr=window.XDomainRequest?new XDomainRequest:new XMLHttpRequest; try{ xhr.onload=function(){ alert([xhr.responseText,xhr]); }; xhr.open("GET","http://ss-o.net/json/wedataAutoPagerize.json"); xhr.send(); }catch(e){ alert(e.message); } }+')();'; document.body.appendChild(scr); })();
// 普通にXHRでhtmlファイルを要求。".json"以外はクロスドメイン制限に引っ掛かる。
javascript:(function(){ var xhr=window.XDomainRequest?new XDomainRequest:new XMLHttpRequest; try{ xhr.onload=function(){ alert([xhr.responseText,xhr]); }; xhr.open("GET","http://ss-o.net/json/wedataAutoPagerize.json"); xhr.send(); }catch(e){ alert(e.message); } })();
// ページ上に埋め込んでも駄目。
javascript:(function(){ var scr=document.createElement('script'); scr.type='text/javascript'; scr.text='('+function(){ var xhr=window.XDomainRequest?new XDomainRequest:new XMLHttpRequest; try{ xhr.onload=function(){ alert([xhr.responseText,xhr]); }; xhr.onerror=function(){ alert('error.'); }; xhr.open("GET","http://tw.muumoo.jp/torchy.html"); xhr.send(); }catch(e){ alert(e.message); } }+')();'; document.body.appendChild(scr); })();