Ajax call fail when I can trying to pass the special characters as data..

Ajax call fail when I can trying to pass the special characters as data..

This is the code I am using to add a comment using Ajax call.

  1. <html>
  2. <head>
  3. <meta charset="utf-8" />
  4.        <meta name="viewport" content="width=device-width, initial-scale=1"/> 
  5.        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.css" />        
  6.        <script type="text/javascript" charset="utf-8" src="js/cordova-1.5.0.js">
  7.        </script>
  8.        <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
  9.        <script src="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.js"></script>
  10.        <script src="js/global.js" type="text/javascript"></script>
  11.     </head>
  12.     <script>
  13.     var msgId = window.localStorage.getItem("clickedId");
  14.     processLogInData = function(){
  15.                  var comment = ($("#comment").val());
  16.                   temp =  'messageId=' + msgId +'&';
  17.                 temp += 'uniqueId=' + device.uuid + '&';
  18.                 temp += 'comments=' + comment;
  19.                 alert(temp);
  20.                 var s= global1 +"rest/Comment/createCommentBO?"+temp;
  21.                 $.ajax({
  22.                     url:global1 +"rest/Comment/createCommentBO?",
  23.                     data: temp,
  24.                     dataType: 'xml',
  25.                     contentType:'application/x-www-form-urlencoded',
  26.                     timeout: 10000,
  27.                     async: false,
  28.                     cache: false,
  29.                     type: 'POST',
  30.                     success: function(data){
  31.                      if($(data).find("isException").text() == "false")
  32.                  {
  33.                                  onTrue();
  34.                  }
  35.                         else
  36.                         {
  37.                          onFalse();
  38.                         }
  39.                   },
  40.                   error:function(XMLHttpRequest,textStatus, errorThrown) {
  41.                   alert("Error message :"+XMLHttpRequest.responseXML);
  42.                   $("#messagelist").append( XMLHttpRequest.responseXML);
  43.                                                                      } 
  44.                  });
  45.           }
  46.                  function onTrue(){
  47.                                 location.replace("comments.html");
  48.                         }   
  49.            function onFalse()
  50.                     {
  51.                         console.log("onFalse Method");
  52.                         alert("Unable to create Comment!");
  53.                                   }
  54.            function cancel(){
  55.            location.replace("comments.html");
  56.            }
  57.     </script>
  58.     <body>
  59.         <div data-role="page" data-theme="a">
  60.              <div data-theme="a" data-role="header">
  61.                <img src="images/logo_header.png" alt="Orange"/>
  62.             </div>
  63.             <div data-role="content">
  64.                <form method="post" name="login" data-ajax="false">
  65.                <label for="textarea"><h3><u>Add Comment</u> : </h3></label>
  66.   <textarea cols="15" rows="15" name="textarea" id="comment"></textarea>
  67.   </form>
  68.   <div>
  69. <div class="ui-block-a"><button type="submit" data-theme="d" onclick="cancel();" data-mini="true" data-icon="delete">Cancel</button></div>
  70.     <div class="ui-block-b"><button type="submit" data-theme="a" onclick="processLogInData();" data-mini="true" data-icon="check" >Submit</button></div>
  71.          </div>
  72.                </div>
  73.               </div> 
  74.     </body>
  75. </html>

When I enter special character as content as pass it to Ajax call I am getting an error :( Ajax call works fine with out any special characters... Is there any way to encode the data before passing it to ajax call???Please help me on this...

Thanks in advance.