简体   繁体   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

I have a php file at path/renderer/renderImage.php that builds and returns a PNG image to the browser. 我在path/renderer/renderImage.php中有一个php文件,该文件可生成PNG图像并将其返回到浏览器。 When I navigate to that URL with my browser, it dumps the correct image on the screen. 当我使用浏览器导航到该URL时,它将在屏幕上转储正确的图像。 But when I try to load that image into a DIV with jQuery using the code below from path/index.html ... 但是当我尝试使用下面的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');
                }
            });

it fills the DIV with strange symbols... 它用奇怪的符号填充DIV ...

在此处输入图片说明

Here is a picture of the directory structure: 这是目录结构的图片:

在此处输入图片说明

Why is the browser interpreting the picture differently on these two pages? 为什么浏览器在这两个页面上对图片的解释不同? What might cause the discrepancy? 可能导致差异的原因是什么?

You are getting symbols instead of an image since you are trying to send binary data without specifying what that data is. 您正在获取符号而不是图像,因为您尝试发送二进制数据而不指定该数据是什么。

Add header to your renderPicture.php file : 标题添加到您的renderPicture.php文件:

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

And it will return the desired png image. 它将返回所需的png图像。

Cheers 干杯

The jQuery .load method will load TEXT/HTML content and insert it into the document. jQuery .load方法将加载TEXT / HTML内容并将其插入文档中。 This is exactly what happens in your case, and is expected behavior. 这正是您遇到的情况,并且是预期的行为。

To load an image via jQuery you can use: 要通过jQuery加载图片,您可以使用:

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

and then make use of the .load (event) to do something: 然后利用.load(事件)来做一些事情:

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

See here: https://stackoverflow.com/a/10863680/2327283 看到这里: https : //stackoverflow.com/a/10863680/2327283

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我已经将网站移至GCE w / LAMP,而我的PHP输出了多余的字符 - I've moved a site to GCE w/ LAMP and my PHP is outputting extra characters 滚动时,为什么我的网站在Chrome中显示滞后? - Why does my website show lag in Chrome when I scroll? 为什么当我单击网页上的发送时,显示的是PHP代码而不是发送电子邮件 - Why when I click send on my webpage does the php code show instead of sending email 当我在首页显示wordpress帖子时,为什么我的静态php代码消失了? - Why does my static php code disappear when i show wordpress post on front page? LAMP当我上传php代码,但直到我在Linux中重新启动apache2服务时浏览器才刷新 - LAMP When i upload php code, but browser is not reflesh until i restart my apache2 services in linux 当我使用JavaScript嵌入时,为什么我的PHP没有激活? - Why does my PHP not activate when I embed it with JavaScript? 当我尝试连接到数据库时,我在灯中的项目不起作用 - my project in lamp not worked when i try to connect to database CSS:当我更改背景图片url()时,它不再遵守background-size:包含直到我调整h&w的大小 - CSS: When I change a background-image url() it no longer respects background-size: contain until I resize h&w Laravel 表格张贴到 url 当我 DD 我的请求时不显示任何内容 - Laravel Form post to url does not show anything when I DD my request 共享时,为什么我的“网站图片”没有显示在Facebook缩略图上? - Why does my Website Pictures does not show on Facebook thumbnail when i share it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM