开发者

How do i load an image with ajax/jquery?

I need to load an image, that is generated by a PHP script. The PHP开发者_JAVA技巧 script accepts a three dimensional array as a parameter and creates from it an image. How do i pass the arguments with ajax? How do i load the image?


There's no way (barring some really hairy <canvas> tag magic) to move data from Javascript into an image, but fortunately, you don't have to. All you have to do generate the URL for the PHP script and assign it to an Image and boom.

var img = new Image();
var img.src = "http://phphost.net/imagegenerate.php?array=3,1,2,3,21,1"; // or whatever


I have done something similar but using ASPX rather than PHP, I used query string params to generate the image, and jQuery to load the image into an existing object in the document:

var chartRootURL = 'http://127.0.0.1/';
var chartQueryParameters = 'inst=APPL&w=900&h=200';
var url = chartRootURL + 'chartImage.aspx?' + chartQueryParameters +
                    '&ts=' + new Date().getMilliseconds();

var img = $('#chartimg').attr('src', url)
                    .load(function (response, status) {
                        $('#chartimg').show();
                    });


How about

document.write("<img src='http://www.aurl.com/generator.php?" + yourParams + "' alt='exciting image here' />")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜