AJAX 加载渲染的 PNG 不起作用
我正在使用自定义 php 脚本和 GD 库根据 POST 中的一些默认值和参数渲染自定义图像。
我正在尝试构建一个自定义表单,以允许用户选择这些参数,然后发送 AJAX 请求来呈现图像并将其发送回并在页面中加载预览。
问题是第一个预览可以工作,但之后我无法加载更多预览。 我总是在预览窗口中看到相同的图像。 但是,我正在将图像写入磁盘,并且更新得很好,所以我认为这是一些 apache 或浏览器缓存。 这是一些代码:
AJAX 请求就像
preview = new Image;
preview.src = url;
$(preview).load(preview.src, imagedata, function() {
$('#gaga-preview-space').html(this);
});
其中 imagedata 是带有 bgcolor 的数组等。我还为每个请求生成一个时间戳,希望它能阻止 apache 缓存响应。 这在其他情况下也有效,但在本次情况下却不起作用。
php 生成脚本如下所示:
// Save file
$file = "/var/www/tribegaga/sites/all/files/gaga_customization/test.png";
$result = imagepng($image, $file);
// Spit out file
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
header("Content-Type: image/png");
imagepng($image);
imagedestroy($image);
您会看到我正在发送一些标头,但这些也不是问题。
也许我认为它是缓存的想法是错误的。 但正如我提到的,生成的 .png 工作正常。
更新:好吧,问题是浏览器不会 POST 来获取图像,而是 GETS,所以现在我的脚本发送一个 GET 字符串,一切都运行得很好。 但我不想有图像 src=http://site/script.php ?string=params&test=foo 等。
如果你们有任何建议,我将不胜感激。
谢谢!!
I am rendering a custom image based on some defaults and parameters in POST with a custom php script and the GD library.
I am trying to build a custom form to allow users to select these parameters and then send an AJAX request to render the image and send it back and load a preview in the page.
The problem is that the first preview works, but after that I can not load any more previews. I just keep seeing the same image in the preview window. However, I am writing the image to disk and that is being updated just fine, so I image this is some apache or browser caching. Here is some code:
AJAX request is like
preview = new Image;
preview.src = url;
$(preview).load(preview.src, imagedata, function() {
$('#gaga-preview-space').html(this);
});
Where imagedata is an array with bgcolor, etc. I am also generating a timestamp for each request, in hopes that it would stop apache from cachine the response. This has worked in other instances, but not this one.
The php generation script looks like:
// Save file
$file = "/var/www/tribegaga/sites/all/files/gaga_customization/test.png";
$result = imagepng($image, $file);
// Spit out file
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
header("Content-Type: image/png");
imagepng($image);
imagedestroy($image);
You'll see I'm sending some headers, but those aren't the issue either.
Perhaps I'm on the wrong path with thinking it is caching. But as I mentioned, the .png generated works fine.
Update: Ok the issue was that the browser doesn't POST to get an image, it GETS, so now my script sends a GET string and it all works wonderfully. But I'd rather not have an image src=http://site/script.php?string=params&test=foo etc.
If you all have any suggstions I'd appreciate it.
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您也可以尝试设置 Expires (过去的日期)和 Last-Modified (当前日期/时间)标头。
You could try setting an Expires (to a date in the past) and Last-Modified (to the current date/time) header too.