這是一個反覆出現的問題,我找到了幾個解決方案。但是沒有一個適合我。
嘗試使用jQuery AJAX發佈表單。Spring MVC:415(Unsupported Media Type)錯誤
注:我昨天發佈了它,認爲這是客戶端問題。嘗試 客戶端的一切可能,但沒有運氣。
春季控制器
@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>
你可以添加'collectFormData()'的示例輸出嗎? –
你正在發佈什麼?服務器是否支持XML POST? –
@WouterHuysentruit我將'collectFormData()'替換爲' '但仍然是相同的錯誤。 –