这是一个反复出现的问题,我找不到一些解决方案。但这些都不适用于我。
尝试使用jQuery AJAX发布表单。
注意:我昨天发布了它,认为这是客户端问题。试着 在客户端一切可能,但没有运气。
Spring Controller
@RequestMapping(value="/save",method=RequestMethod.POST,consumes="application/json")
@ResponseBody public String handleSave(@RequestBody String formData)
{
System.out.println(formData);
}
jQuery请求(尝试了评论中人们建议的所有内容)
如果我发送data:$(this).serialize()
和contentType:application/json
$('form').submit(function () {
$.ajax({
url: $(this).attr('action'),
type: 'POST',
data: collectFormData(),
headers: {
"Content-Type":"text/xml"
},
dataType: 'xml;charset=utf-8',
success: function (data) {
alert('data:'+data)
},
error: function (jqXHR, textStatus, errorThrown) {
alert('jqXHR:'+jqXHR+'\n'+'textStatus:'+'\n'+textStatus+'errorThrown:'+errorThrown);
}
});
return false;
});
collectFormData()
function collectFormData()
{
$rootElement = $.createElement($('form').attr('name'));
$('form').find('div.section').each(function(index, section) {
var $sectionElement = $.createElement($(section).attr('name'));
console.log('Section Name is:'+$sectionElement);
$(section).find('input').each(function(i, field) {
var $fieldName = $.createElement($(field).attr('name'));
$fieldName.text($(field).val());
$sectionElement.append($fieldName);
});
$rootElement.append($sectionElement);
});
console.log('Form XML is '+$rootElement.html());
return $rootElement.html();
}
HTML
<div class="section" name="amount">
<span class="section-title">Personal Information</span>
<div class="span6 form-inline">
<label class="pocLabel">
<span style="color:red;">*</span>
Amount Of Insurance</label>
<input type="text" name="amount-amountOfInsurance" required="" id="123456" onblur="validateWithPRASE(this)">
</div>
<div class="span6 form-inline">
<label class="pocLabel">
<span style="color:red;">*</span>
Customer First Name</label>
<input type="text" name="amount-firstName" required="">
</div>
<div class="span6 form-inline">
<label class="pocLabel">
<span style="color:red;">*</span>
Customer Last Name</label>
<input type="text" name="amount-lastName" required="">
</div>
<div class="span6 form-inline">
<label class="pocLabel">
<span style="color:red;">*</span>
Middle intials</label>
<input type="text" name="amount-middleIntials" required="">
</div>
<div class="span6 form-inline">
<label class="pocLabel">
<span style="color:red;">*</span>
Date</label>
<input type="text" id="datepicker" class="datepicker" name="date">
</div>
<div class="span6 form-inline">
<label class="pocLabel">
Street Address</label>
<input type="text" name="amount-address">
</div>
<div class="span6 form-inline">
<label class="pocLabel">
City</label>
<input type="text" name="amount-city">
</div>
<div class="span6 form-inline">
<label class="pocLabel">
State</label>
<input type="text" name="amount-state">
</div>
<div class="span6 form-inline">
<label class="pocLabel">
State 2</label>
<input type="text" name="amount-state">
</div>
<div class="row-fluid show-grid">
</div>
</div>
答案 0 :(得分:1)
您的控制器只接受JSON。它永远不会消耗XML。尝试使它同时消耗:
@RequestMapping(value="/save",
method=RequestMethod.POST,
consumes={"application/json", "application/xml"})
答案 1 :(得分:0)
我认为charset
应该在contentType
而不是dataType
中定义,如下所示:
$('form').submit(function () {
$.ajax({
url: $(this).attr('action'),
type: 'POST',
data: collectFormData(),
dataType: 'xml',
contentType: "text/xml; charset=utf-8",
success: function (data) {
alert('data:'+data)
},
error: function (jqXHR, textStatus, errorThrown) {
alert('jqXHR:'+jqXHR+'\n'+'textStatus:'+'\n'+textStatus+'errorThrown:'+errorThrown);
}
});
return false;
});
同时验证您的XML数据是否以版本和编码开头:
<?xml version="1.0" encoding="UTF-8"?>