I need to POST short messages to the server. 开发者_如何学C 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}
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 }}
import urllib;
urllib.urlencode("string");
I hope this helps!
Take a look at this: http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
So : becomes %3A
BTW...most modern browsers do this automatically.
精彩评论