Himanshu Yadav
Himanshu Yadav

Reputation: 13585

Spring MVC: 415 (Unsupported Media Type) Error

It is somewhat recurring issue and I have found few solutions.But none of it work for me.
Trying to post a form with jQuery AJAX.

Note: I posted it yesterday, thinking it is a client side issue. Tried everything possible on client side but 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 everything what people suggested in comments)

It works fine if I send data:$(this).serialize() and 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>

Upvotes: 0

Views: 4863

Answers (2)

acdcjunior
acdcjunior

Reputation: 135822

Your controller only accepts JSON. It will never consume a XML.Try making it consume both:

@RequestMapping(value="/save",
                method=RequestMethod.POST,
                consumes={"application/json", "application/xml"})

Upvotes: 1

huysentruitw
huysentruitw

Reputation: 28151

I think charset should be defined in contentType instead of dataType, like this:

$('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;
});

Also verify that your XML data starts with version and encoding:

<?xml version="1.0" encoding="UTF-8"?>

Upvotes: 0

Related Questions