简体   繁体   English

尝试通过ajax发布到数据库时转义URI。 我究竟做错了什么?

[英]Trying to escape URI while posting via ajax to database. What am I doing wrong?

I'm grabbing the src attribute from an image tag and posting it to my database via an ajax call which is then used to form an XML file. 我从图像标记中获取src属性,并通过ajax调用将其发布到我的数据库,然后将其用于形成XML文件。 I really need to escape this image path and seem to be quite stumped on how to do this. 我真的需要逃离这个图像路径,似乎对如何做到这一点感到非常难过。 Trying to use the encodeURIComponent without much luck. 尝试使用encodeURIComponent没有太多运气。 Below is my code. 以下是我的代码。 Thanks for any help! 谢谢你的帮助!

$('.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>';
});

You must use encodeURI. 你必须使用encodeURI。 Because encodeURIComponent isn´t good for full paths. 因为encodeURIComponent不适合完整路径。


Try to use a xml encode function (encodeXml) clearing your xml from amps: 尝试使用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];
});
}

From here: http://dracoblue.net/dev/encodedecode-special-xml-characters-in-javascript/155/ 从这里: http//dracoblue.net/dev/encodedecode-special-xml-characters-in-javascript/155/

hard to say since it looks all good to me. 很难说因为它看起来对我很好。 tried reproducing and is fine in my browser. 尝试复制,在我的浏览器中很好。

try changing javascript library (jquery?) and try another browser. 尝试更改javascript库(jquery?)并尝试其他浏览器。 And try breaking down the string to variables to nail the problem. 并尝试将字符串分解为变量以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM