使用 ajax 乱码文本显示图像


Display image using ajax garbled text

希望你能帮助我,我有生成图像的PHP脚本(使用curl获取图像)它是通过ajax调用的,当我直接访问PHP脚本(不使用ajax)图像成功生成,但是当通过ajax调用时,它显示乱码文本。你知道这其中的原因吗?

PHP脚本:

header('Content-Type: image/jpeg');
header('Content-Length: ' . get_size($songname));
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_HTTPGET,1);
curl_setopt($ch, CURLOPT_URL,$url);
curl_exec($ch);
curl_close($ch);

Javascript:

$.post(baseUrl+"getphoto.php",{photo:path }).done(function( data ) {
    console.log('#thumb_'+id);
    //garbled text appear on firebug
}});

动态创建映像因此您需要添加内容类型的标头

header('Content-Type: image/jpeg');

获取照片.php