我正在使用GD库绘制图像,之后我将使用jquery ajax获取该图像。但是,当我更新图像时,jquery ajax没有得到更新的图像,而当我检查目录时,图像会更新。因此,在这种情况下,请帮助我如何在jquery ajax响应中获得更新的图像。
我从php文件获取响应的jquery代码如下:
jQuery.ajax({
type: "POST",
url: "create.php",
dataType:"text",
data:myData,
success:function(response){
//location.reload();
var result = response.split("|");
document.getElementById('img').innerHTML = result[0]; // This is the image
document.getElementById('download').innerHTML = result[1]; // this is a href link for some other purpose
},
error:function (xhr, ajaxOptions, thrownError){
alert(thrownError);
}
});
在这种情况下请帮帮我。感谢
如果不确定是否要缓存,请尝试create.php?123451234(randomNumber)
。
你能把result
的输出加到你的答案中吗?
我认为主要的错误是你设置了innerHTML
,它取代了你需要的更多。
为什么不像那样使用
jQuery.('#img').attr('src', result[0]);
更新:
查看您的代码,替换此代码:
<div id="img"><img src="images/quotepreivewimg.gif" alt="" /></div>
通过这个:
<div><img id="img" src="images/quotepreivewimg.gif" alt="" /></div>
然后使用上面的jQuery.attr函数加载图片
这无法工作:
document.getElementById('img').innerHTML
图像没有任何内部html代码,必须使用src属性:
document.getElementById('img').src = result[0];
更改
document.getElementById('img').innerHTML = result[0];
至:
document.getElementById('img').src = result[0];
更新版本:
jQuery.ajax({
type: "POST",
url: "create.php",
dataType:"text",
data:myData,
success:function(response){
//location.reload();
var result = response.split("|");
document.getElementById('img').src = result[0]; // we need the src property here
document.getElementById('download').innerHTML = result[1]; // this is a href link for some other purpose
},
error:function (xhr, ajaxOptions, thrownError){
alert(thrownError);
}
});
希望这能有所帮助。