我正在使用ajax()将POST请求发送到php页面。这是代码:
date = $.trim($('#date').val())
expiry = $.trim($('#expiry').val())
priority = $.trim($('#priority').val())
note = $.trim($('#note_text').val())
$.ajax({
type: "POST",
url: "client?method=addNote&id=10",
data: "date="+date+"&expiry="+expiry+"&priority="+priority+"¬e="+note,
success: function(msg){
alert(msg);
}
});
我的问题是名为note的最后一个变量可能包含许多“奇怪”字符,例如:& % $ / : ; , .
我已经看到php页面没有正确接收所有“note”字符串。如果它找到(&)它js“截断”字符串。 我该如何编码该文字?
答案 0 :(得分:4)
不要传递字符串,只需传递数据。
data: { date: date, expiry: expiry, priority: priority, note: note },
如果你要传递一个字符串,那么你手工构建一个URI并且与jQuery无关,所以你可以使用encodeURIComponent。
答案 1 :(得分:1)
尝试:
note = encodeURIComponent($.trim($('#note_text').val()));
有关encodeURIComponent
:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent