我有一个用户表格。如果用户键入带有'或"作为字符串一部分的字符串,我没有问题。表单已提交并正确保存到数据库。我的问题是当我重新加载页面时(所有条目都可以修改,并在显示之前加载到JSP的列表中)。在加载页面时,我收到一条错误消息:
'
"
missing ) after argument list 'Caroline's message', \n
我需要做些什么来转义该字符串以在前端显示它?
这是我在前端使用的代码,以读取数据并将其存储在JavaScript对象中。我不确定我应该在哪里逃脱。导致问题的字段是c.getComName:
communications[<%=i%>][1] = new CommObject('<%=c.getComId()%>', '<%=c.getComName()%>');
生成的HTML更新:
communications[0][1] = new CommObject('101', 'Caroline's Message');
使用Apache StringEscapeUtils.escapeJavaScript函数。
Escapes the characters in a String using JavaScript String rules. Escapes any values it finds into their JavaScript String form. Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.) So a tab becomes the characters '\\' and 't'.
Escapes the characters in a String using JavaScript String rules. Escapes any values it finds into their JavaScript String form. Deals correctly with quotes and control-chars (tab, backslash, cr, ff,
etc.)
So a tab becomes the characters '\\' and 't'.