Re: [問題] 讀取外部title

看板Ajax作者 (死城盜賊)時間14年前 (2010/07/30 13:56), 編輯推噓2(2014)
留言16則, 3人參與, 最新討論串2/2 (看更多)
※ 引述《driftseed (阿輝)》之銘言: : 檔案:http://www.sendspace.com/file/togq72 (959B) : 截圖:http://i.imgur.com/e4xl6.jpg
: 在javascript想要得到一個"外部"網站的標題內容不知要從何下手 : 外部網頁僅透過 : xmlhttp.Open("GET",URL, false); : 來測試網址是否有效,並不打算在框架中載入那個網頁 : 不過xmlhttp.Open測試的時候也算是一種載入網頁吧? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "" rel="nofollow">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="" rel="nofollow">http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>測試</title> <script type="text/javascript"> function request(url, callback, method) { var xmlHttp = createXMLHTTPObject(); if ( ! xmlHttp) return; xmlHttp.open(method, url, true); xmlHttp.onreadystatechange = function () { if (xmlHttp.readyState != 4) return; if (xmlHttp.status != 200 && xmlHttp.status != 304) { alert('HTTP請求錯誤 ' + xmlHttp.status); return; } callback(xmlHttp); } if (xmlHttp.readyState == 4) return; xmlHttp.send(null); } function createXMLHTTPObject() { var XMLHttpFactories = [ function () {return new XMLHttpRequest()}, function () {return new ActiveXObject("Msxml2.XMLHTTP")}, function () {return new ActiveXObject("Msxml3.XMLHTTP")}, function () {return new ActiveXObject("Microsoft.XMLHTTP")}, ]; var xmlhttp = false; for (var i = 0; i < XMLHttpFactories.length; i ++ ) { try { xmlhttp = XMLHttpFactories[i](); } catch (e) { continue; } break; } return xmlhttp; } function checkUrl(url) { try { if(url) { request(url, function (response) { document.getElementById("responseHere").innerHTML=response.responseText.match(/(<title>).*<\/title>/gi)[0].replace(/<.?title>/gi,""); }, "GET"); } else throw "Url is undefined"; } catch(e) { document.getElementById("responseHere").innerHTML=((e) || (e.description)); } } </script> </head> <body> <form> 輸入網址: <input type="text" id="txtUrl" value="http://tw.yahoo.com/" /> <input type="submit" value="test" onclick="checkUrl(document.getElementById('txtUrl').value);return false;" /> </form> <ul> <li>測試網址是否有效 </li> <li>有效--得到該網址的標題--結束</li> <li>無效--結束</li> </ul> <div id="responseHere"></div> </body> </html> 奇怪的是 firefox 與 chrome 都不支援 到底是哪裡出問題... -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.172.94.13

07/30 14:19, , 1F
google後聽說是MIME Type的問題
07/30 14:19, 1F

07/30 14:19, , 2F
可是加上 xmlHttp.overrideMimeType('text/html');還是無
07/30 14:19, 2F

07/30 14:20, , 3F
解...
07/30 14:20, 3F

07/30 18:14, , 4F
本機測試或在線上?本機好像 xmlHttp.status = 0
07/30 18:14, 4F

07/30 18:18, , 5F
Chrome不知哪版開始本機的ajax變成不能順利執行...我有
07/30 18:18, 5F

07/30 18:19, , 6F
一個程式以前本機線上都能跑,現在Chrome 5.0.375.125只
07/30 18:19, 6F

07/30 18:19, , 7F
有線上能跑...
07/30 18:19, 7F

07/30 18:22, , 8F
本G 難道說線上就可以了嗎?!
07/30 18:22, 8F

08/09 07:14, , 9F
Ajax不能取得跨網域的資料,也不能使用本地端的方式
08/09 07:14, 9F

08/09 07:16, , 10F
請Google一下有關Ajax安全性的議題來釐清觀念
08/09 07:16, 10F

08/10 20:51, , 11F
本機端執行如果使用相對路徑是可以的,至少大多數瀏覽器
08/10 20:51, 11F

08/10 20:51, , 12F
都支援,不過多少會有些安全控管,像Fx如果沒特別設定權
08/10 20:51, 12F

08/10 20:51, , 13F
限就只能讀取同層或下層資料夾等等
08/10 20:51, 13F

08/10 21:10, , 14F
IE不能讀 IE不能讀 IE不能讀 xml\a.xml
08/10 21:10, 14F

08/10 21:10, , 15F
同層不知道 可是我放下一層不能讀= =
08/10 21:10, 15F

08/10 21:10, , 16F
不過反正我有阿帕契~
08/10 21:10, 16F
文章代碼(AID): #1CKcbnHX (Ajax)
討論串 (同標題文章)
文章代碼(AID): #1CKcbnHX (Ajax)