问题描述
我在将XML数据从Ajax发送到ASP.NET MVC时遇到问题.数据未发送.Ajax代码:
I have problem sending XML data fron Ajax to ASP.NET MVC. Data is not sent.Ajax code:
`function SendXmlToServer(ServerXml) {
$.ajax({ url: "/Home/XmlData",
type: "POST",
processData: false,
data: { ResXml: ServerXml }, dataType: "xml",
success: function () {
alert("Successful");
return false;
}
})
}`
ASP.NET MVC代码:
ASP.NET MVC code:
[HttpPost]
public ActionResult XmlData(string ResXml)
{
return null;
}
为什么ResXml变量为空?
Why ResXml variable has null?
推荐答案
默认模型绑定不适用于将 processData
设置为 false
的情况.如果 ServerXml
是XML字符串,则删除它应该可以使其工作:
The default model binding doesn't work with processData
set to false
. If ServerXml
is a string of XML, removing this should make it work:
function SendXmlToServer(ServerXml) {
$.ajax({ url: "/Home/XmlData",
type: "POST",
data: { ResXml: ServerXml }, dataType: "xml",
success: function () {
alert("Successful");
return false;
}
});
}
您还必须将 ValidateInput
属性添加到您的操作方法中,因为通常不允许使用"HTML标记":
You'll also have to add the ValidateInput
attribute to your action method, because normally "HTML markup" isn't allowed:
[HttpPost]
[ValidateInput(false)]
public ActionResult XmlData(string ResXml)
{
return null;
}
或者,您可以使用自定义模型绑定来无缝地反序列化XML,如此博客文章.
Alternatively, you could use custom model binding to seamlessly deserialize the XML, as explained in this blog post.
这篇关于从Ajax向XML数据发布到ASP.NET MVC的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!