Before sending text input to a server to be stored in a MySQL database using XML, I first escape the "&" characters:
var copyright = copyright.replace(/&/g,"&");
The wrapped XML data block is then sent to the server through jQuery's ajax
method:
var copyright = copyright.replace(/&/g,"&"),
xml = "<request><session>"+session+"</session><space>"+space_id+"</space><view>"+view_id+"</view><copyright>"+copyright+"</copyright></request>",
url = "hidden URL";
$.ajax({
type: "POST",
url: url,
contentType: "text/xml; charset=UTF-8",
dataType: "xml;charset=UTF-8",
data: xml
});
To display previously saved content within a webpage after retrieving it from the database, another ajax call is made:
$.ajax({
type: "POST",
url: url,
dataType: 'xml',
data: xmlString,
success: function(xml) {
var XML = $(xml);
// Process the retrieved data
},
error: function(jqXHR, textStatus, errorThrown) {
var XML = $(jqXHR.responseText);
console.log("error: "+textStatus+"\n"+errorThrown);
}
});
If an ampersand was saved and the page with the content is loaded, the ajax call returns an error due to invalid XML:
error: parsererror
Error: Invalid XML: <?xml version="1.0" encoding="UTF-8"?>... (omitted for brevity)
Is there an issue with how I am escaping characters or could it be something else causing this problem?
While this question might appear familiar, the character escaping ensures accuracy when storing content, yet the issue persists even with added escape characters.