这是一个反复出现的问题,我发现很少有解决方案。但没有一个对我有用。
尝试使用 jQuery AJAX 发布表单。
注意:我昨天发布了它,认为这是一个客户端问题。在客户端尝试了所有可能的方法,但没有运气。
弹簧控制器
@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;
});
收集表单数据()
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>