How to avoid special characters in a query string sent through jQuery ajax?

I need to send short POST messages to the server. Sometimes they have special characters, as in this example:

&message=Joining a game of Commands & Colors: Ancients. 

How to avoid special characters from the query string?

I am using Django. Here is 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 understand that POST executes the jQuery 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, am I not passing (urlencoded) correctly?

UPDATE: after the problem arose using jQuery, I was able to fix it by replacing the purpose of the data variables:

  data: {"mid": mid, "message": message, "facebook": facebook, "name": name, "bgg_id": bgg_id, "thumbnail": thumbnail} 
+4
source share
3 answers

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 }} 
+6
source
 import urllib; urllib.urlencode("string"); 

Hope this helps!

+3
source

Take a look at this: http://www.blooberry.com/indexdot/html/topics/urlencoding.htm

So: it becomes% 3A
BTW ... most modern browsers do this automatically.

0
source

Source: https://habr.com/ru/post/1338528/


All Articles