简体   繁体   中英

How to escape special characters from a textarea using jQuery post()

I have a form that is composed of a couple of text fields and a textarea for order notes/comments . The problem that I have is that every time I type a special character in the textarea the ajax request stops working. I've tried to escape the special characters using escape, escapeURIComponent, etc, but nothing seems to work. Removing the special characters works, but that doesn't solve my problem. Any suggestions? below is a snippet of the code I have:

$.post(getPost('addComponent'), {
                componentType:$('#componentType').val(),
                productQty:$('#productQty').val(),
                comments:$("#orderNotes").val()
            }, function(data, statusCode) {
        if (statusCode === 'success') {
            updateComponentContainer(data);
        } else {
            return false;
        }
    }, 'json');

Have a look at jQuery.serialize()

http://api.jquery.com/serialize/

** EDIT **

Look at this jsFiddle:

http://jsfiddle.net/nPdW9/

Not sure what the special characters are, but base64 encoding should solve the problem.

http://www.webtoolkit.info/javascript-base64.html

and if you like to try before you buy, the demo page!

http://www.webtoolkit.info/demo/javascript-base64

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