Search code examples
c#asp.net-mvcurl.action

How to html encode a text before sending it unto the server?


I have the below code in my MVC view which gets the contents of a TextArea control and makes a request to a url (action) then displays the result in another text box:

var input = $('#tbInput').val(); // it can have some invalid characters such as +, #, etc

        var encodedInput = // how?; 

        $.ajax({
            url: url,
            success: function (data) {
                alert(data);
                $('#tbResult').html(data);
            },
            error: function (request, status, error) {
                alert('An error occured: ' + error);
            },
            cache: false
        });

If I only send the input data, the invalid characters are just removed so I'd need to html encode the text then sending it?

The input should appear as below:

http://www.mydomain.com/?i=

Thanks,


Solution

  • Let jQuery do that:

        $.ajax({
            url: url,
            data: { something: input },