如何转义通过 JQuery ajax 函数发送的查询字符串中的特殊字符?
我需要将短消息发布到服务器。有时它们包含特殊字符,如本示例所示:
&message=Joining a game of Commands & Colors: Ancients.
How do I escape Special characters from a query string?
我正在使用姜戈。这是我需要编码的字段:
<textarea class="text-area" id="message" name="message" rows="3" cols="30">
Joining a game of {{ game.name }}.
</textarea>
更新:我意识到 POST 正在通过 JQuery 的 ajax 函数:
$("#checkin-button").click(function() {
var mid = $("input#mid").val();
var message = $("textarea#message").val();
var facebook = $('input#facebook').is(':checked');
var name = $("input#name").val();
var bgg_id = $("input#bgg-id").val();
var thumbnail = $("input#thumbnail").val();
var dataString = 'mid='+mid+'&message='+message+'&facebook='+facebook+'&name='+name+'&bgg_id='+bgg_id+'&thumbnail='+thumbnail;
$.ajax({
type: "POST",
url: "/game-checkin",
data: dataString,
所以我没有正确地传递它(urlencoded),对吧?
更新:在意识到问题出在 JQuery 之后,我能够通过将数据变量赋值替换为以下内容来修复它:
data: {"mid": mid, "message": message, "facebook": facebook,
"name": name, "bgg_id": bgg_id, "thumbnail": thumbnail}
I need to POST short messages to the server. Sometimes they have special characters, like in this example:
&message=Joining a game of Commands & Colors: Ancients.
How do I escape special characters from a query string?
I'm using Django. Here's the field I need to encode:
<textarea class="text-area" id="message" name="message" rows="3" cols="30">
Joining a game of {{ game.name }}.
</textarea>
UPDATE: I realize that the POST is going through JQuery's ajax function:
$("#checkin-button").click(function() {
var mid = $("input#mid").val();
var message = $("textarea#message").val();
var facebook = $('input#facebook').is(':checked');
var name = $("input#name").val();
var bgg_id = $("input#bgg-id").val();
var thumbnail = $("input#thumbnail").val();
var dataString = 'mid='+mid+'&message='+message+'&facebook='+facebook+'&name='+name+'&bgg_id='+bgg_id+'&thumbnail='+thumbnail;
$.ajax({
type: "POST",
url: "/game-checkin",
data: dataString,
So I'm not passing that correctly (urlencoded) right?
UPDATE: after realizing the issue was with JQuery, I was able to fix it by replacing the data variable assignment with:
data: {"mid": mid, "message": message, "facebook": facebook,
"name": name, "bgg_id": bgg_id, "thumbnail": thumbnail}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Python:
Django:
Python:
Django:
我希望这有帮助!
I hope this helps!
看看这个:
http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
所以:变成 %3A
顺便说一句......大多数现代浏览器都会自动执行此操作。
Take a look at this:
http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
So : becomes %3A
BTW...most modern browsers do this automatically.