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.

Answers


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

Need Your Help

How do you get WCF serialization to preserve a non-default Comparer on a generic Dictionary?

c# wcf serialization dictionary iequalitycomparer

Suppose we start from scratch in Visual Studio 2010 and add a 'WCF Service Aplication'. We add this method and implementation:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.