如何转义通过 JQuery ajax 函数发送的查询字符串中的特殊字符?

发布于 2024-10-16 12:43:09 字数 1292 浏览 5 评论 0原文

我需要将短消息发布到服务器。有时它们包含特殊字符,如本示例所示:

&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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

桃扇骨 2024-10-23 12:43:09

Python

>>> urllib.urlencode({'message': 'Joining a game of Commands & Colors: Ancients.'})
'message=Joining+a+game+of+Commands+%26+Colors%3A+Ancients.'

Django

message={{ message|urlencode }}

Python:

>>> urllib.urlencode({'message': 'Joining a game of Commands & Colors: Ancients.'})
'message=Joining+a+game+of+Commands+%26+Colors%3A+Ancients.'

Django:

message={{ message|urlencode }}
街角卖回忆 2024-10-23 12:43:09
import urllib;
urllib.urlencode("string");

我希望这有帮助!

import urllib;
urllib.urlencode("string");

I hope this helps!

坠似风落 2024-10-23 12:43:09

看看这个:
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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文