简体   繁体   中英

Error 415 sending JSON to Spring MVC with AJAX

I'm trying to send a JSON object to Spring MVC.

My AJAX function take the input fields from a form and send the JSON to my controller, but I am getting a 415 error code.

<script type="text/javascript">

        $(document).ready(function () {

            $('#hitoDetail').submit(function (event) {

                var id = $('id').val;
                var hitoNumber = $('hitoNumber').val;
                var title = $('title').val;
                var subtitle = $('subtitle').val;
                var date = $('date').val;
                var latitude = $('latitude').val;
                var longitude = $('longitude').val;
                var json = {
                    "id": id, "hitoNumber": hitoNumber, "title": title,
                    "subtitle": subtitle, "date": date, "latitude": latitude,
                    "longitude": longitude
                };

                $.ajax({
                    url: $("#hitoDetail").attr("action"),
                    data: JSON.stringify(json),
                    type: "POST",
                    headers: {
                        'Accept': 'application/json',
                        'Content-Type': 'application/json'
                    },
                });
                event.preventDefault();
            });
        });

    </script>

Here is my controller:

@RequestMapping(value="/modifyHito.htm", method=RequestMethod.POST)
public @ResponseBody String modifyHito(HttpServletRequest request, @RequestBody Hito hitoForm) {
    hitoManager.modifyHito(hitoForm);
    return "success";
}

Where is the error?

Did you set up your message converter properly? Absence of the latter is one of the most common reasons for 415 error code. You may simply register message converter by putting <mvc:annotation-driven /> in your Spring XML config.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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