Springmvc and Ajax Interactions FAQ

Source: Internet
Author: User
Tags parse error

This is the low-level mistake I made when I personally wrote the blog system because of personal negligence.

But it's a good thing to make a mistake and help summarize.

1. Add @requestbody before parameter

If you are using AJAX interaction, you must add this contenttype: ' Application/json;charset=utf-8 '

Otherwise, this exception will occur:


Error message: org.springframework.web.HttpMediaTypeNotSupportedException:Content type ' application/x-www-form-urlencoded; Charset=utf-8 ' not supported

This type of delivery is not supported because it is not defined in the foreground and indicates that the content passed must be JSON. Sometimes special backstage with @requestbody as the parameter, must add contenttype in Ajax: ' Application/json;charset=utf-8 ', plus this means that you have passed the object defined as a JSON type, @ Requestbody also supports only JSON types. So it is easy to understand why this error occurred. In particular, we are the developers of the army, many of whom have such a person, for never encountered errors, copy paste to Baidu to search, usually before people have encountered, through the form of blog to record the error message and how to solve it, and why it happened. But some of our friends only look at how to solve, as to why it will happen never to ignore.

Error message: Org.springframework.http.converter.HttpMessageNotReadableException:JSON parse error:unrecognized token ' email ': Was expecting (' true ', ' false ' or ' null '); Nested exception is com.fasterxml.jackson.core.JsonParseException:Unrecognized token ' e-mail ': Was expecting (' true ', ' False ' or ' null ')

This error is due to the fact that the JSON object parameters you have defined are not fully written, and this error is caused by the @requestbody receive parameter in the background, must be written in the data corresponding value in Ajax json.stringify

Example:

That's right, it's not going to happen.

$.ajax ({url: "/blog-web/user/register", type: "POST", ContentType: ' Applicatio N/json;charset=utf-8 ', data:JSON.stringify (data), DataType: ' JSON ', succes S:function (data) {if (data.returncode== "333333") {layui.use (' layer '),                                                function () {var layer = Layui.layer;                  Layer.alert (data.returnmsg, {icon:5});                                      });                          }else if (data.returncode== "222222") {layui.use (' layer ', function () {                                                    var layer = Layui.layer;                    Layer.alert (data.returnmsg, {icon:5});                                               }); }else if (data.returncode== "111111") {layui.use (' layer ', function () {var LAyer = Layui.layer;                  Layer.alert (data.returnmsg, {icon:5});                  });                           }else if (data.returncode== "000000") {layui.use (' layer ', function () {                                                      var layer = Layui.layer;                                              Layer.alert ("Please come in Soon" +data.returnmsg,{icon:1});                      });                    SetTimeout (() = {view ();                                    }, 10000); }},error:function () {layui.use (' layer ', function () {var                                                layer = Layui.layer;                                          Layer.alert ("with exception, please contact the Administrator");                  }); }            });

Springmvc and Ajax Interactions FAQ

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.