The Light
The Light

Reputation: 27021

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,

Upvotes: 1

Views: 487

Answers (1)

SLaks
SLaks

Reputation: 888303

Let jQuery do that:

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

Upvotes: 1

Related Questions