2011-03-30 102 views
1
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); 

上述代碼使用jQuery加載加載圖像。它適用於text.content爲普通字符的情況。但是,當text.content包含URL中不允許的特殊字符(如'&'字符,甚至編碼爲'%26')時,將無法加載圖像。如何用jQuery ajax返回圖片?

因此,我正在尋找一種方法來發送一些數據使用ajax,允許使用特殊字符,同時也能夠返回圖像。

如果我使用$ .ajax(),返回值是否包含二進制圖像流?如果不是,可以用特殊字符調用ajax並同時返回圖像的其他方法是什麼?

+4

爲什麼不把圖像存儲在亞馬遜或其他東西(而不是數據庫)然後存儲網址?是不是更容易? – corroded 2011-03-30 05:21:36

回答

0

嘗試escape()http://www.javascripter.net/faq/escape.htmjavascript urlencode()http://phpjs.org/functions/urlencode:573

希望這有助於。

+0

嘗試了兩個(並重試),它將'&'轉換爲'%26',但它仍不被視爲字符串。 – kenfai 2011-03-31 03:01:29

+0

我在這裏看到的一件事是, 'webroot +'?drawtxt ='+ text.content'同時具有+和。通常+用於在JS中追加字符串,但是。在php – 2011-03-31 08:36:26

+0

的追加函數中,點是JS Dot Notation的對象。因爲我用alert()進行了測試,所以沒有錯。 – kenfai 2011-04-01 07:05:37