Javascript Spring MVC:无法将xml DOM发送到控制器
尝试用表单值构建xml。早些时候,我是在jQuery的帮助下构建它的。jQuery有一个bug,然后我必须用纯javascript构建XML。但现在当我提交表单时,浏览器挂起,请求未到达控制器。Javascript Spring MVC:无法将xml DOM发送到控制器,javascript,jquery,xml,spring,spring-mvc,Javascript,Jquery,Xml,Spring,Spring Mvc,尝试用表单值构建xml。早些时候,我是在jQuery的帮助下构建它的。jQuery有一个bug,然后我必须用纯javascript构建XML。但现在当我提交表单时,浏览器挂起,请求未到达控制器。 控制器 @RequestMapping(value="/save",method=RequestMethod.POST,consumes={"application/json", "application/xml", "text/xml", "text/plain"}) @Respons
控制器
@RequestMapping(value="/save",method=RequestMethod.POST,consumes={"application/json", "application/xml", "text/xml", "text/plain"})
@ResponseBody public String handleSave(@RequestBody String formData)
{
System.out.println("comes here");
System.out.println(formData);
return "SUCCESS";
}
jQuery
$('form').submit(function () {
$.ajax({
url: $(this).attr('action'),
type: 'POST',
data: collectFormData(),
headers: {
"Content-Type":"application/xml"
},
dataType: 'application/xml',
success: function (data) {
alert('Success:'+data)
},
error: function (jqXHR, textStatus, errorThrown) {
console.log('jqXHR:'+jqXHR+'\n'+'textStatus:'+'\n'+textStatus+'errorThrown:'+errorThrown);
}
});
return false;
});
CollectFormData
function collectFormData()
{
$rootElement = $('<FormXMLDoxument/>');
xmlDoc = document.implementation.createDocument("", "", null);
root = xmlDoc.createElement($('form').attr('name'));
$('form').find('div.section').each(function(index, section) {
sectionElement = xmlDoc.createElement($(section).attr('name'));
$(section).find('input').each(function(i, field) {
fieldElement = xmlDoc.createElement($(field).attr('name'));
fieldText=xmlDoc.createTextNode($(field).val());
fieldElement.appendChild(fieldText);
sectionElement.appendChild(fieldElement);
});
root.appendChild(sectionElement);
});
console.log((new XMLSerializer()).serializeToString(root));
return root;
}
function collectFormData()
{
$rootElement = $('<FormXMLDoxument/>');
$formElement = $.createElement($('form').attr('name'));
$('form').find('div.section').each(function(index, section) {
var $sectionElement = $.createElement($(section).attr('name'));
$(section).find('input').each(function(i, field) {
console.log($(field).attr('name'));
var $fieldName = $.createElement($(field).attr('name'));
$fieldName.text($(field).val());
$sectionElement.append($fieldName);
});
$formElement.append($sectionElement);
});
$rootElement.append($formElement);
console.log('Form XML is '+$rootElement.html());
return $rootElement.html();
}
函数collectFormData()
{
$rootElement=$('');
xmlDoc=document.implementation.createDocument(“,”,null);
root=xmlDoc.createElement($('form').attr('name'));
$('form')。查找('div.section')。每个(函数(索引,节){
sectionElement=xmlDoc.createElement($(section.attr('name'));
$(节).find('input')。每个(函数(i,字段){
fieldElement=xmlDoc.createElement($(field.attr('name'));
fieldText=xmlDoc.createTextNode($(field.val());
fieldElement.appendChild(fieldText);
sectionElement.appendChild(fieldElement);
});
根.appendChild(sectionElement);
});
log((新的XMLSerializer()).serializeToString(根));
返回根;
}
如果我使用jQuery实现collectFormData,一切都会正常工作,但随后我在元素名称上松开了驼峰式大小写。旧collectFormData
function collectFormData()
{
$rootElement = $('<FormXMLDoxument/>');
xmlDoc = document.implementation.createDocument("", "", null);
root = xmlDoc.createElement($('form').attr('name'));
$('form').find('div.section').each(function(index, section) {
sectionElement = xmlDoc.createElement($(section).attr('name'));
$(section).find('input').each(function(i, field) {
fieldElement = xmlDoc.createElement($(field).attr('name'));
fieldText=xmlDoc.createTextNode($(field).val());
fieldElement.appendChild(fieldText);
sectionElement.appendChild(fieldElement);
});
root.appendChild(sectionElement);
});
console.log((new XMLSerializer()).serializeToString(root));
return root;
}
function collectFormData()
{
$rootElement = $('<FormXMLDoxument/>');
$formElement = $.createElement($('form').attr('name'));
$('form').find('div.section').each(function(index, section) {
var $sectionElement = $.createElement($(section).attr('name'));
$(section).find('input').each(function(i, field) {
console.log($(field).attr('name'));
var $fieldName = $.createElement($(field).attr('name'));
$fieldName.text($(field).val());
$sectionElement.append($fieldName);
});
$formElement.append($sectionElement);
});
$rootElement.append($formElement);
console.log('Form XML is '+$rootElement.html());
return $rootElement.html();
}
函数collectFormData()
{
$rootElement=$('');
$formElement=$.createElement($('form').attr('name'));
$('form')。查找('div.section')。每个(函数(索引,节){
var$sectionElement=$.createElement($(section.attr('name'));
$(节).find('input')。每个(函数(i,字段){
console.log($(field.attr('name'));
var$fieldName=$.createElement($(field.attr('name'));
$fieldName.text($(field.val());
$sectionElement.append($fieldName);
});
$formElement.append($sectionElement);
});
$rootElement.append($formElement);
log('表单XML为'+$rootElement.html());
返回$rootElement.html();
}
我的问题与jQuery有关在这里跳出框框思考。。。我不知道如何立即解决你最初的问题。但是将数据作为XML提交是一项要求吗?您是否可以将表单数据作为bean提交,并在将其放入控制器后将其转换为XML?是的。这里没有定义XML结构。XML元素根据文档中提供的字段名进行保护。然后,我将把表单xml值映射到现有模型。现有的模型很复杂,一对一映射是不可能的。Himanshu,你有没有弄明白这一点?如果在服务器上填充一个bean并将键/值对解码为xml会起作用,请告诉我,我可以给您一些示例代码。文本字段中是否包含所需的元素?如果是这样的话,生成xml将很容易…@wSchmidt是的,我已经让它工作了,但现在控制器返回另一个错误。我已经贴了