简体   繁体   中英

Ajax call fail when parameter contain html tag

I have this ajax :

 $.ajax({
      url: '/PostComment/AddComment',
      type: 'POST',
      dataType: 'json',
      cache: false,
      data: { "PostId": PostId, "CommentText": CommentText },
      success: function (data){
           alert('Ok');
      }
 });

The problem is that when the CommentText variable contain any html tag the ajax call fail. I know this is a strange problem, but that is what happen.

Try sending encoded values to the server side:

commentText = encodeURIComponent(commentText);

On the server side, if you are using Java, then you can do:

String commentStr = URLDecoder.decode(request.getParameter("commentText"), "UTF-8");

In javascript :

JSON.stringify(CommentText));

See : 4 Things You Must Do When Putting HTML in JSON

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM