2011-07-25 19 views
3

我從圖像標記中獲取src屬性,並通過ajax調用將其發佈到我的數據庫,然後用它來形成一個XML文件。我真的需要擺脫這種形象的道路,似乎很難在如何做到這一點。嘗試使用encodeURIComponent,但沒有多少運氣。以下是我的代碼。謝謝你的幫助!試圖在通過ajax發佈到數據庫時轉義URI。我究竟做錯了什麼?

$('.drag-elem').each(function() { 
    var xml = '<clother><id>' + $(this).children('img').attr('class') + 
     '</id><title>' + $(this).children('img').attr('alt') + '</title><z-index>' + 
     $(this).css('z-index') + '</z-index><top>' + $(this).css('top') + 
     '</top><left>' + $(this).css('left') + '</left><file>' + 
     encodeURIComponent($(this).children('img').attr('src')) + 
     '</file></clother>'; 
}); 
+1

是什麼,輸出什麼樣子? – Joe

+0

輸出將是這個。正如你可以從節點看到IMG的網址是不是很XML友好 '<?XML版本= 「1.0」?> 產品5316 琥珀母老虎燈罩 355px 113px http://www.domain.com/images/thumb/phpThumb.php?src=http://www.domain.com/uploads/products/712SP_GLD.jpg&w=95&h=98&q = 90&far = C&bg = FFFFFF ' –

+0

從哪裏輸出? PHP還是JS? – Dor

回答

0

很難說,因爲它看起來對我都很好。嘗試複製,並在我的瀏覽器罰款。

嘗試更改JavaScript庫(jquery?)並嘗試其他瀏覽器。並嘗試將字符串分解爲變量來解決問題。

1

您必須使用encodeURI。因爲encodeURIComponent不適合完整路徑。


嘗試使用XML編碼功能(encodeXml)從安培清除您的xml:

var xml_special_to_escaped_one_map = { 
'&': '&amp;', 
'"': '&quot;', 
'<': '&lt;', 
'>': '&gt;' 
}; 

var escaped_one_to_xml_special_map = { 
'&amp;': '&', 
'&quot;': '"', 
'&lt;': '<', 
'&gt;': '>' 
}; 

function encodeXml(string) { 
return string.replace(/([\&"<>])/g, function(str, item) { 
return xml_special_to_escaped_one_map[item]; 
}); 
}; 

function decodeXml(string) { 
return string.replace(/(&quot;|&lt;|&gt;|&amp;)/g, 
function(str, item) { 
return escaped_one_to_xml_special_map[item]; 
}); 
} 

從這裏:http://dracoblue.net/dev/encodedecode-special-xml-characters-in-javascript/155/

+0

我剛剛使用encodeURI嘗試了這一點,並且整個過程一時死亡點擊&符號&。之後沒有任何內容被髮布。 –