下面是我的Ajax代码,加载时间为3分15秒。有什么问题吗?
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<Script type="text/javascript">
function im()
{
alert('iam in');
var Name=document.getElementById("Name").value;
var pointsize=document.getElementById("pointsize").value;
var bckclr=document.getElementById("bckclr").value;
var color=document.getElementById("color").value;
var bcolor=document.getElementById("bcolor").value;
var url='Name='+Name+'&pointsize='+pointsize+'&bckclr='+bckclr+'&color='+color+'&bcolor='+bcolor;
alert("srihost.com/2.php?"+ url);
alert(url);
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("Div_Im").innerHTML=xmlhttp.responseText;
document.getElementById("Div_Im").style.border="2px solid #A5ACB2";
}
}
xmlhttp.open("GET","2.php?"+ url,true);
xmlhttp.send();
}
</script>
</head>
<body>
Enter Name: <input type="text" id="Name" onchange="im()" value="yourname" name="Name" />
pointsize: <input type="text" id="pointsize" onchange="im()"
value="50" name="pointsize" />
BackGround Color: <input type="text" id="bckclr" value="red"
onchange="im()" name="bckclr" />
FontColor: <input type="text" id="color" value="white"
onchange="im()" name="color" />
Border Color: <input type="text" id="bcolor" value="blue"
onchange="im()" name="bcolor" />
<div id="Div_Im">
replace me
</div>
</body>
</html>
这是我的服务器页面代码:
$Name=$_GET["Name"];
$pointsize=$_GET["pointsize"];
$bckclr=$_GET["bckclr"];
$color=$_GET["color"];
$bcolor=$_GET["bcolor"];
$filename = 'im.png';
$font='Times-Roman';
$cmd = " -background $bckclr -pointsize $pointsize -font $font -fill $color ".
" -strokewidth 1 -stroke $bcolor label:\"$Name\" ";
exec("convert $cmd $filename");
echo('<img src="'.$filename.'">');
I assume you tried the same command yourself and it was faster.
So, make sure whenever you do testing on the command line, to always use the same user "su www-data" for Debian for example.
Then always have a log for debugging, so you can make sure that the parameter are all set.
[EDIT] What platform do you run on? What version of php? what version of image magik?
You should add a few more details in order to let people help you!
Well two reasons can make place. First you are executing ajax on text box change. Which send ajax request again and again. You should call ajax on only when some button/link/submit clicked so only one ajax call be sent out. Also on php side you using exec and convert function which can be very heavy if first case is also true.
In server-side the execution may be very slow. Run a browser debugger like Firebug that let you watch the network requests. Verify that the issue is the time between when the HTTP request is sent to your server and when the response is received.