weixin_33686714 2011-10-09 00:26 采纳率: 0%
浏览 361

AJAX:将图表数据显示为IMG

I want to show a (chart) image using AJAX. I'm not sure what's wrong, but I'm getting the following 'error' and an incorrect image: "|xtt�I������{饗BBBN�:��}�̛7oРA7n�0l߾} QQQIIIeee�aLHH������ضm��wm���v��U�&�Z���o�# Art]]]����{���#""��'���v|�ҥKqqq���ح�~;11�ȑ#����޺u��ںm6O�7o���.��ի��?~Ȑ!��~��۷��O�0A.�cv�����TäR)�� ����˗{N����5<��&0� ���ҷo��@�NХ<0�j�0��=���]�t��۷�j�T*5�\����۳g�F�����gfm���ݻ�'OF....."

The code I'm using:

ajax_select.php:

<html>
<head>
<script type="text/javascript">

var xmlhttp;

function showUser(str,age)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url="test_ajax.php";
url=url+"?q="+str+"&a="+age;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("txtHint").innerHTML="<IMG SRC='" + xmlhttp.responseText + "'/>";
}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

</script>
</head>
<body>

<form>
<select id="users" name="users">
<option value="">Select a person:</option>
<?php

$con = mysql_connect(***);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("***", $con);

$sql="SELECT ***";

$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
  {

  }

mysql_close($con);
?>
</select>

<select id="age" name="age">
<option value="">Select a person:</option>
<?php

$con = mysql_connect('***');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("***", $con);

$sql="SELECT ***";

$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
  {
  }

mysql_close($con);

?>
</select>

<input type='button' value='Refresh Data' onclick="showUser(document.getElementById('users').value,document.getElementById('age').value)">
</form>

<br><br>
<div id="txtHint"><b>chart will be displayed here.</b></div>

</body>
</html> 

ajax_select_NEW.php:

<script type="text/javascript">

var xmlhttp;

function showUser(str,age)
{
var url = 'test_ajax.php';
url += '?q=' + str + '&a=' + age + '&sid=' + Math.random();

document.getElementById('txtHint').innerHTML = '<img src="' + url + '" />';

xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }

xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}


function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

</script>

test_ajax.php:

<?php
/* Include the pData class */
include("class/pData.class.php");
include("class/pDraw.class.php");
include("class/pImage.class.php");

/* Get user from AJAX resquest */
$user_id=$_GET["q"];

$q=$_GET["q"];
$a=$_GET["a"];

/* Create the pData object */
$MyData = new pData();  

/* Connect to the MySQL database */
$db = mysql_connect("***");
mysql_select_db("***",$db);




/* Build the query that will returns the data to graph */

$Requete = "

SELECT ***
";

***
/* Render the picture (choose the best way) */
$myPicture->autoOutput("examples/example.drawBarChart.png");

?> 

I have hard coded the variables in the SQL code for now. (in test_ajax.php) So if I open that page it just shows the correct chart image. But when I open the ajax_select.php page I get the error in the picture above. (so it's not a wrong chart code information, since it's okay if I open the php page directly)

I have searched a lot, but can't find the solution. Hopefully someone can help me here, would be much appreciated!

  • 写回答

1条回答 默认 最新

  • weixin_33690963 2011-10-09 08:59
    关注

    You're trying to put the binary image data into the src attribute of the img. This attribute is meant for the source URL of the image, you can do this entirely without XmlHttpRequest, just insert your image by using test_ajax.php as the src.

    function showUser(str, age) {
        var url = 'test_ajax.php';
        url += '?q=' + str + '&a=' + age + '&sid=' + Math.random();
    
        document.getElementById('txtHint').innerHTML = '<img src="' + url + '" />';
    }
    

    As for the broken rendering of the image, have you included a Content-Type-header?

    header('Content-Type: image/png');
    $myPicture->autoOutput("examples/example.drawBarChart.png");
    

    This is what ajax_select_NEW.php should look like:

    <script type="text/javascript">
    function showUser(str, age) {
        var url = 'test_ajax.php';
        url += '?q=' + str + '&a=' + age + '&sid=' + Math.random();
    
        document.getElementById('txtHint').innerHTML = '<img src="' + url + '" />';
    }
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 slam rangenet++配置
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊