jquery - Spring MVC: 415 (Unsupported Media Type) Error -
it recurring issue , have found few solutions.but none of work me.
trying post form jquery ajax.
note: posted yesterday, thinking client side issue. tried possible on client side no luck.
spring controller
@requestmapping(value="/save",method=requestmethod.post,consumes="application/json") @responsebody public string handlesave(@requestbody string formdata) { system.out.println(formdata); }
jquery request(tried people suggested in comments)
it works fine if send 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 '+$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>
your controller accepts json. never consume xml.try making consume both:
@requestmapping(value="/save", method=requestmethod.post, consumes={"application/json", "application/xml"})
Comments
Post a Comment