Search code examples
phpjqueryajaxposthtmlspecialchars

Send special characters through AJAX ( "&lt" , "&gt" , "&amp" )


I send some test through jquery's ajax request. This text contains some special characters like "&lt" , "&gt" , "&amp", which stand for <, >, and &.

$.ajax({
 type: "POST",
 url: "page.php",
 data: "content="+txt });

Unfortunately, the string is not well transmitted. Only the beginning is transmitted, and it is cut at the first special character.

For instance, if I send "blablabla&ltgrogrogro", the server only receive "blablabla"

How can I fix that?

PS: The special characters actually end with a ";", I did not put it because otherwise they are not well displayed.


Solution

  • & represents another variable in your data query string. So, you should send it by using a JavaScript object like so:

    $.ajax({
        type: "POST",
        url: "page.php",
        data: {
            "content": txt
        }
    });