2013-08-23 11 views
0

我有一個PHP文件path/renderer/renderImage.php,它構建並返回一個PNG圖像到瀏覽器。當我使用瀏覽器導航到該URL時,它會在屏幕上顯示正確的圖像。但是,當我嘗試使用下面的代碼路徑/ index.html的到圖像加載到使用jQuery一個DIV ...爲什麼我的LAMP堆棧顯示我的圖像,當我導航到它帶有一個URL但沒有加載它時帶有jQuery和PHP

<!DOCTYPE html> 
<html> 
    <head> 
     <meta charset='utf-8'/> 
     .... 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
     <script src="../jquery.colorbox.js"></script> 
     <script> 
      $.colorbox({ 
       width:"30%", 
       inline:true, 
       href:"#inline_content", 
       onClosed: function() { 
        $("#myPic").load('./renderer/renderPicture.php'); 
        $('#myPic').css('display','inline'); 
       } 
      }); 

它填滿了奇怪的符號的格...

enter image description here

這裏是目錄結構的圖片:

enter image description here

爲什麼瀏覽器不同的解釋畫面上兩頁?什麼可能導致差異?

+0

而不是.load,而是嘗試使用.html。 .load是ajax請求的簡寫。 – stefancarlton

+1

@stefancarlton,如何有一個文件的路徑作爲html內容的幫助? – mishik

+1

ack,謝謝@mishik。 Plonker瞬間由我!您需要創建一個img元素,然後將SRC放在那裏。 – stefancarlton

回答

2

您正在獲取符號而不是圖像,因爲您正在嘗試發送二進制數據而未指定該數據是什麼。

添加renderPicture.php文件:

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

,它將返回所需的png圖片。

乾杯

1

jQuery的.load方法將加載TEXT/HTML內容,並將其插入到文檔中。這正是你的情況,並且是預期的行爲。

要通過jQuery加載圖像,你可以使用:

$('<img src="./renderer/renderPicture.php">') 

,然後利用.load(事件)的做一些事情:

$('<img src="./renderer/renderPicture.php">').load(function() { 
    $(this).appendTo('#myPic'); 
}); 

在這裏看到:https://stackoverflow.com/a/10863680/2327283

相關問題