Ajax 板


LINE

總共有三個檔案分別為.js,.php,.html,名稱皆為 .js檔負責html與.php中間傳輸的過程 .php檔負責產生圖案 .html負則顯示.php檔所產生之圖案 problem:如果.php檔不是產生圖案的話,.html檔可以正常顯示,但若 .php是產生圖案的話,則.html會顯示亂碼。 自己猜想問題應該是出現在.js檔下面幾行的地方 var response = xmlHttp.responseText; myDiv = document.getElementById("myDivElement"); myDiv.innerHTML = response; 因為我是經由.php產生圖,所以xmlHttp.responseText這裡所收到的內容 應該是一個圖吧@@;接著myDiv.innerHTML應該是指插入HTML語言 但我插入的卻不是@@,所以應該是這裡的問題。 但對WEB語法不熟@@;請各位大大幫幫忙啊 阿李阿鬥 ^^ 以下為程式碼 .js var xmlHttp = createXmlHttpRequestObject(); var count=0; //-----creates an XMLHttpRequest instance start---- function createXmlHttpRequestObject() { // will store the reference to the XMLHttpRequest object var xmlHttp; // this should work for all browsers except IE6 and older try { // try to create XMLHttpRequest object xmlHttp = new XMLHttpRequest(); } catch(e) { // assume IE6 or older var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0", "MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"); for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) { try { xmlHttp = new ActiveXObject(XmlHttpVersions[i]); } catch (e) {} } } if (!xmlHttp) alert("Error creating the XMLHttpRequest object."); else return xmlHttp; } //---------creates an XMLHttpRequest instance end------------------- function process() { //---------------count the GD number start--------------------- if(count==400) count=0; else count=count+1; //----------------count the GD number end---------------------- // -------only continue if xmlHttp isn't void start------------------- if (xmlHttp) { // try to connect to the server try { // initiate server access xmlHttp.open("GET", "proxyping.php?count=" + count, true); xmlHttp.onreadystatechange = handleRequestStateChange; xmlHttp.send(null); setTimeout('process()',100); } // display the error in case of failure catch (e) { alert("Can't connect to server:\n" + e.toString()); } } } //---------only continue if xmlHttp isn't void end-------------------- // -----function called when the state of the HTTP request changes-------- function handleRequestStateChange() { // when readyState is 4, we are ready to read the server response if (xmlHttp.readyState == 4) { // continue only if HTTP status is "OK" if (xmlHttp.status == 200) { try { // do something with the response from the server handleServerResponse(); } catch(e) { // display error message alert("Error reading the response: " + e.toString()); } } else { // display status message alert("There was a problem retrieving the data:\n" + xmlHttp.statusText); } } } // -------------function called when the state of the HTTP request changes---------- // -------------handles the response received from the server------------ function handleServerResponse() { var response = xmlHttp.responseText; myDiv = document.getElementById("myDivElement"); myDiv.innerHTML=response; } // ---------------handles the response received from the server------------------- .html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html> <head> <title>Practical AJAX: Accessing Remote Server through Proxy PHP Script</title> <script type="text/javascript" src="proxyping.js"></script> </head> <body onload="process()"> <div id="myDivElement" /> </body> </html> .php <?php // load the error handling module require_once('error_handler.php'); print $count=$_GET['count']; header("Content-type:image/gif"); //設定header以讓瀏覽器將檔案視為圖檔 $im=ImageCreate(800,300); //產生一個空白圖 $red=ImageColorAllocate($im,0,0,255); //第一個ImagecolorAllocate決定背景顏色 $white=ImageColorAllocate($im,255,0,255); $yellow=ImageColorAllocate($im,255,255,255); // Imageline($im,1,255,1,$array_line[0],$yellow); //為一線條影像,im像是 一個容器,Imageline像是內容物;前面四個數字表示 //x,y啟始位址與結束位置 $fp=fopen("Menu.txt","r"); $j=0; while($line=fgets($fp,$count)) { $array_line=explode(' ',$line); //using the explode function divide the line from string to arry. for($i=0,$num_count=count($array_line);$i<$num_count;$i++) { Imageline($im,(2+$i*1.5+$j*1.5),275,(2+$i*1.5+$j*1.5),275-$array_line[$i],$yellow); } $j=$j+$i; Imagestring($im,5,$j+50,$j+50,$j,$yellow); } Imagegif($im); //輸出圖檔 ImageDestroy($im); //消毀圖片 ?> --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.122.79.88
1F:推 alpe:<img id="xx" src="img"> document[xx].src="new image" 03/20 13:01
※ 編輯: adddream 來自: 140.122.79.88 (03/20 13:28)







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:e-shopping站內搜尋

TOP