ajax獲取外部資料時,遇見document.write的問題
阿新 • • 發佈:2019-01-06
通過script的方式獲取的資料顯示正常,但因為需要顯示為英文,所以需要將資料獲取下來再改為英文,這裡想到的是ajax的方式。
抓包獲取資料
亂碼部分是因為編碼的問題。document.write("<a href='http://www.zzsky.cn' target='_blank'> շ <img src='http://www.zzsky.cn/images/count/5/0.gif' border='0'><img src='http://www.zzsky.cn/images/count/5/0.gif' border='0'><img src='http://www.zzsky.cn/images/count/5/2.gif' border='0'><img src='http://www.zzsky.cn/images/count/5/5.gif' border='0'><img src='http://www.zzsky.cn/images/count/5/1.gif' border='0'><img src='http://www.zzsky.cn/images/count/5/9.gif' border='0'> շ <img src='http://www.zzsky.cn/images/count/5/0.gif' border='0'><img src='http://www.zzsky.cn/images/count/5/0.gif' border='0'><img src='http://www.zzsky.cn/images/count/5/1.gif' border='0'><img src='http://www.zzsky.cn/images/count/5/2.gif' border='0'><img src='http://www.zzsky.cn/images/count/5/8.gif' border='0'><img src='http://www.zzsky.cn/images/count/5/3.gif' border='0'> վ <img src='http://www.zzsky.cn/images/count/5/0.gif' border='0'><img src='http://www.zzsky.cn/images/count/5/0.gif' border='0'><img src='http://www.zzsky.cn/images/count/5/6.gif' border='0'><img src='http://www.zzsky.cn/images/count/5/0.gif' border='0'><img src='http://www.zzsky.cn/images/count/5/4.gif' border='0'><img src='http://www.zzsky.cn/images/count/5/9.gif' border='0'></a>")
獲取資料的時候,因為資料中有document.write的方法,會有錯誤發生,造成內容無法顯示。
count.asp:2 Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.這個時候需要自己重寫document.write,讓document.write執行我們想要的效果。
定義一個html標籤,在自己重寫的document.write中將獲取的內容插入到定義的標籤中。
function newwrite(str){
//str為獲取的資料,這裡可以自定義資料的處理。
};
document.write = newwrite;