简体   繁体   English

Spring MVC:415(不支持的媒体类型)错误

[英]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. 尝试使用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) jQuery请求 (尝试了人们在评论中提出的所有内容)

It works fine if I send data:$(this).serialize() and contentType:application/json 如果我发送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() 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 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 only accepts JSON. 您的控制器仅接受JSON。 It will never consume a XML.Try making it consume both: 它永远不会使用XML。尝试使其同时使用:

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

I think charset should be defined in contentType instead of dataType , like this: 我认为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;
});

Also verify that your XML data starts with version and encoding: 还要确认您的XML数据以版本和编码开头:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM