Currently, I am facing an issue while attempting to send an XML string from the client side in HTML/JavaScript to the ASP.NET MVC server side. Interestingly, when sending a regular "non-XML" string, the transfer is successful.
Here is the relevant JavaScript code on the client side:
function TransferXmlDataToServer() {
var sXml = "<Tag>This is an XML test string.</Tag>"
$.ajax({
type: "POST",
url: '@Url.Action("TransferXMLData", "Home")',
data: { sInputXml: sXml },
dataType: "json",
success: function(sReturnValue) {
alert("Value returned from server is: " + sReturnValue);
},
error: function() {
alert("There was an error on the server side");
}
})
};
The corresponding function in the MVC Home controller on the server side looks like this:
public JsonResult TransferXMLData(string sInputXml) {
// The arguments' name must match those used in the View's Ajax call
return Json("Success");
}
When invoking TransferXmlDataToServer from the client side, the message There was an error on the server side is displayed. Debugging print statements within TransferXMLData server-side are not triggered, indicating that the function is not being accessed.
Interestingly, switching the XML string:
sXml = "<Tag>This is an XML test string.</Tag>"
with:
sXml = "This is a test string."
results in everything working as expected.
Some additional points:
- This problem occurred in testing with IE11 and Edge browsers.
- I also attempted converting the XML string to Serialized JSON before transmission, but encountered the same issue.
Your insights and guidance would be greatly appreciated. Thank you.