Fix plus sign (+) is replaced with space using ajax
Code Snippet
- DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>Fix plus sign (+) is replaced with space using ajaxtitle>
- <meta name="author" content="http://123code.blogspot.com" />
- <script src="js/jquery-1.2.6.pack.js" type="text/javascript">script>
- <script type="text/javascript">
- function update() {
- var comment = $('#txtComment').val();
- $.ajax({
- type: 'POST',
- data: 'comment=' + encodeURIComponent(comment),
- success: function(data) {
- $('#result').html(data);
- }
- });
- return false;
- }
- script>
- head>
- <body>
- <form id="form1">
- <div>
- <h1>Fix plus sign (+) is replaced with space using ajaxh1>
- <input id="txtComment" type="text" />
- <input id="btnUpdate" type="button" value="button" onclick="return update();" />
- div>
- <div>Result:div>
- <div id="result">div>
- form>
- body>
- html>
In line 13, i use encodeURIComponent to solve the problem. To see the problem, just replace encodeURIComponent with escape function.
Ps: Don't trust user input. The data should be encoded before sending to server side. I don't know why the browsers replace plus sign (+) with space using escape function, please let me known.
Happy coding!
Comments