簡體   English   中英

為什么當我使用URL導航到它時,我的LAMP堆棧會顯示我的圖像,而當我使用jQuery和PHP加載它時卻不顯示我的圖像

[英]why does my LAMP stack show my image when I navigate to it w/ a URL but not when I load it w/jQuery and PHP

我在path/renderer/renderImage.php中有一個php文件,該文件可生成PNG圖像並將其返回到瀏覽器。 當我使用瀏覽器導航到該URL時,它將在屏幕上轉儲正確的圖像。 但是當我嘗試使用下面的path / index.html中的代碼將圖像加載到jQuery的DIV中時...

<!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');
                }
            });

它用奇怪的符號填充DIV ...

在此處輸入圖片說明

這是目錄結構的圖片:

在此處輸入圖片說明

為什么瀏覽器在這兩個頁面上對圖片的解釋不同? 可能導致差異的原因是什么?

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

標題添加到您的renderPicture.php文件:

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

它將返回所需的png圖像。

干杯

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM