通过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.

& 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
    }
});

This is by design: If you pass a string to data, it will be treated as form URLencoded data (like a query string). In that format, & signifies the start of a new parameter. Ampersands in data need to be URLencoded.

You would need to either URLencode your data before transmission - in PHP, the right function would be urlencode() - or pass an array that jQuery will encode automatically:

data: {"content": txt }