I am trying to pass an XML string from the HTML/JavaScript client side to the ASP.NET MVC server side. The problem is that the XML string never reaches the server, whereas an ordinary "non-XML" string will be successfully transferred.
我试图将XML字符串从HTML / JavaScript客户端传递到ASP.NET MVC服务器端。问题是XML字符串永远不会到达服务器,而普通的“非XML”字符串将成功传输。
The relevant JavaScript code on the client side is the following:
客户端上的相关JavaScript代码如下:
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");
}
})
};
This is the corresponding function in the MVC Home controller on the server side:
这是服务器端MVC Home控制器中的相应功能:
public JsonResult TransferXMLData(string sInputXml) {
// The arguments' name must match those used in the View's Ajax call
return Json("Success");
}
When the TransferXmlDataToServer is invoked from the client side, the There was an error on the server side message is displayed. I have put some debugging printing statements in the TransferXMLData on the server side that are not invoked, showing that one does not even enter in this function.
从客户端调用TransferXmlDataToServer时,显示服务器端的消息错误。我在服务器端的TransferXMLData中放了一些调试打印语句,这些语句没有被调用,表明一个甚至没有进入这个函数。
On the other hand, when
另一方面,当
sXml = "<Tag>This is an XML test string.</Tag>"
is replaced by
被替换为
sXml = "This is a test string."
everything works as expected.
一切都按预期工作。
Additional notes:
- This was tried with IE11 and Edge.
- I tried to convert the XML string to Serialized Json prior to sending it to the server, to no avail.
这是用IE11和Edge尝试的。
我试图在将XML字符串发送到服务器之前将其转换为Serialized Json,但无济于事。
I would greatly appreciate knowing what I am doing wrong.
我非常感谢知道我做错了什么。
Thanks a lot.
非常感谢。
1 个解决方案
#1
0
This is because by default asp.net protects against content that looks like HTML mark-up being sent to a controller action un-encoded. You need to decorate your action with the ValidateInputAttribute
to let the content through:
这是因为默认情况下,asp.net可以防止看起来像HTML标记的内容被发送到未编码的控制器操作。您需要使用ValidateInputAttribute修饰您的操作,以使内容通过:
[ValidateInput(false)]
public JsonResult TransferXMLData(string sInputXml)
{
// The arguments' name must match those used in the View's Ajax call
return Json("Success");
}
#1
0
This is because by default asp.net protects against content that looks like HTML mark-up being sent to a controller action un-encoded. You need to decorate your action with the ValidateInputAttribute
to let the content through:
这是因为默认情况下,asp.net可以防止看起来像HTML标记的内容被发送到未编码的控制器操作。您需要使用ValidateInputAttribute修饰您的操作,以使内容通过:
[ValidateInput(false)]
public JsonResult TransferXMLData(string sInputXml)
{
// The arguments' name must match those used in the View's Ajax call
return Json("Success");
}