作者adddream (哆啦A孟)
看板Ajax
标题[问题] 画图的问题
时间Tue Mar 20 12:45:57 2007
总共有三个档案分别为.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)