如何在jquery ajax响应中获取更新的图像?

我正在使用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);
    }
});

请帮助我,谢谢!

This cannot work:

document.getElementById('img').innerHTML

An image doesn't have any inner html code, you have to use the src property:

document.getElementById('img').src = result[0];

not sure about caching, try create.php?123451234(randomNumber) instead.

can you also add the output of result to your answer?

I think the main error is that you set the innerHTML which replaces more than you need.

why not use like

jQuery.('#img').attr('src', result[0]);

Update:

Looking at your code, replace this:

<div id="img"><img src="images/quotepreivewimg.gif" alt="" /></div>

by this:

<div><img id="img" src="images/quotepreivewimg.gif" alt="" /></div>

then use the above jQuery.attr function to load the picture

Change

document.getElementById('img').innerHTML = result[0];

to:

document.getElementById('img').src = result[0];

Updated version:

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

Hope this helps.