2015-01-14 20 views
2

希望你能幫助我,我有PHP腳本生成圖像(使用curl獲取圖像)它是通過ajax調用,當我直接訪問php腳本(不使用ajax)圖像成功生成但是當通過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 
}}); 

回答

0

創建圖像動態 因此你需要添加的Content-Type頭

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

到getphoto.php

+0

我已經有內容類型看到我的上面的代碼,當我訪問瀏覽器上的getphoto.php,但通過ajax亂碼文本顯示 – darrel

+0

ya.you必須在img元素的src中設置recived數據時生成圖像。你不能直接顯示圖像源 –

+0

你的意思是亂碼文本是正常的?它不會顯示Ajax響應的實際圖像? – darrel