开发者

How to return an image with jQuery ajax?

开发者 https://www.devze.com 2023-02-20 21:34 出处:网络
var txtimg = new Image(); //img holder $(txtimg).load(function () { $(\"#div\").append(this); }).error(function () {
var txtimg = new Image(); //img holder
    $(txtimg).load(function () {
        $("#div").append(this);
    }).error(function () {
        // notify the user that the image could not be loaded
        alert('error loading text');
    }).attr('src', webroot + '?drawtxt='+text.content);

The above code loads an image using jQuery load. It works when text.content are normal characters. However, the image won't load when text.content contains special characters which are not permitted in the URL, such as '&' character, even encoded as '%26'.

Therefore, I'm looking for a way to send some data using ajax that permits the use of special characters, and at the same time is able to return the image as well.

If I use $.ajax(), can the return value contains the binary image stream? If no, what are other ways that can do an aj开发者_JAVA百科ax call with special characters and return an image at the same time?


Try escape() http://www.javascripter.net/faq/escape.htm or javascript urlencode() http://phpjs.org/functions/urlencode:573

Hope this helps.

0

精彩评论

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