我在将对象插入 mysql 表中时遇到一些问题。一个是谷歌纬度/液化天然气对象(使用VARCHAR):
JSON.stringify(coordinates)
[-90.02532485,35.04651167]
还有一些网址:
mysqlEscape(aurl);
function mysqlEscape(stringToEscape){
return stringToEscape
.replace("\", "\\")
.replace("'", "\'")
.replace(""", "\"")
.replace("n", "\n")
.replace("r", "\r")
.replace("x00", "\x00")
.replace("x1a", "\x1a");
};
error on http://pbs.twimg.com/media/Ci5x4CZXAAAZCZc.jpg
如何插入这些?
要在JS中转义字符串,最好使用encodeURIComponent
函数而不是滚动自己的解决方案。
console.log(encodeURIComponent('http://pbs.twimg.com/media/Ci5x4CZXAAAZCZc.jpg'));
// output -> http%3A%2F%2Fpbs.twimg.com%2Fmedia%2FCi5x4CZXAAAZCZc.jpg
有关更多详细信息,请参阅encodeURIComponent
文档。