作者greenfish27 ()
看板Web_Design
標題[問題] iframe如何送access token
時間Tue May 29 19:14:33 2018
(補上作法於下方,謝謝Gold740716)
大家好,我正在修改一個問題遇到了困難,想聽聽前輩們的各式建議 Q Q
以前iframe取跨網站的網頁A都運作良好
最近網頁A增加了token認證,所以要修改iframe呼叫
我不知道該如何在 iframe.src = A網頁url; 時就帶有token
試過使用 var xhr = new XMLHttpRequest();
給定 xhr.setRequestHeader('Authorization', 'Bearer 123');
iframe.srcdoc = this.reponse;
雖然的確可以取回頁面資訊呈現,不過版面卻亂糟糟的orz
**************調整方式**************
使用srcdoc填入html並不會觸發script內容,這是我版面排版亂掉的原因
所以將上方
亮紫部份改用contentWindow.document.write即可
iframe.contentWindow.document.open();
iframe.contentWindow.document.write(this.response);
iframe.contentWindow.document.close();
ref: https://harttle.land/2016/04/14/iframe-script-injection.html
************************************
或是我不該使用iframe 該換用怎樣的容器呢?
(嗚嗚 公司也不允許用jq等套件,有看到網路一些是用.ajax post成功後
去修改iframe.src的方法我就沒試了..)
謝謝~
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.57.231.227
※ 文章網址: https://webptt.com/m.aspx?n=bbs/Web_Design/M.1527592475.A.E65.html
1F:推 Gold740716: 搞不好用 service worker onfetch 事件 05/29 21:07
2F:→ Gold740716: 可以攔到 iframe 發的 request ,再手動加 header 05/29 21:08
3F:推 Gold740716: 我之前是習慣用 iframe.contentDocument.write(html) 05/29 21:17
4F:→ greenfish27: 謝謝Gold大大!onfetch感覺太黑科技 我應該一時半刻 05/29 22:52
5F:→ greenfish27: 駕馭不了,不過document.write好像很有希望感(握拳 05/29 22:52
6F:→ greenfish27: ) 原來許多26外掛廣告都是用這招呀 奮鬥試驗中 05/29 22:52
※ 編輯: greenfish27 (220.133.189.2), 05/29/2018 23:32:58
7F:推 Gold740716: 或覺 document.write 和 srcdoc 應該差不多啊 05/31 20:07
8F:→ Gold740716: ^感覺 05/31 20:07
9F:→ Gold740716: 另外如果 .write() 後沒有 .close() 會一直轉圈圈。 05/31 20:08
10F:→ Gold740716: 正常應該是 .open() .write(html) .close() 05/31 20:09