Spring MVC cannot get the parameters and values of Ajax post

Source: Internet
Author: User

How can it be this way

A very simple idea, Ajax submits a form in post, Spring MVC parsing. Yet again and again the printing of NULL tortured me for a whole day ...

The final Solution now appears to be a clear question, "just disconsolate" ...

It is boundless! It is boundless! It is boundless!

Second, simple original sin

Ajax commits the following code:

1<script type="Text/javascript">2 $ (document). Ready (function () {3$("#submit"). Click (function (e) {4 E.preventdefault ();5         varobj = $ ( This);6         varName = $ ("input[name= ' name ']"). Val ();7         varPhone = $ ("input[name= ' phone ']"). Val ();8 $.ajax ({9Url:"Userask",TenType:"POST", OneContentType:"Application/json;charset=utf-8", A data: {Name:name,phone:phone}, -           //dataType: "Text", - success:function (result, status, req) { the$(". Noticeinfo"). CSS ("Display","Block"); -           }, - error:function (req, status, reason) { -$(". Noticeinfo"). CSS ("Display","Block"). Text ('Error:'+reason); +           } -         }) +         return false; A       }) at     }); -</script>

Third, the tangled backstage

By the way, review the values of Spring MVC:

1. Get the value in the URL by annotating pathvariable.

1  @RequestMapping (value="user/{id}/{name}", method=requestmethod.get)2 public    string Mycontroller (@PathVariable string ID, @PathVariable string name, Modelmap model) { /c9>3      ... 4      return " OK " ; 5  }

2. Get the value passed through the annotation requestparam.

1@RequestMapping (value ="/test", method =requestmethod.post)2  PublicString MyTest (@RequestParam ("name") String name, @RequestParam ("Phone") String phone, modelmap model) {3 ...4     return "OK";5}

3. Use the source httpservletrequest to get the value yourself.

1@RequestMapping (value="/test"METHOD =requestmethod.post)2  PublicStringGet(HttpServletRequest request, httpservletresponse response) {3String name = Request.getparameter ("name")); 4     return "OK"; 5}

4. Use annotations to modelattribute the parameters in the form directly to Pojo.

Note: For the time being, I use JSON to serialize the situation.

The above method I tried various, has been ruthless print null.

Four, do you suspect that the background has received data?

1 bufferedreader BR;2         Try {3BR =Req.getreader ();4String str, wholestr ="";5              while(str = br.readline ())! =NULL){6Wholestr + =str;7             }8System. out. println (WHOLESTR);9}Catch(IOException e) {Ten e.printstacktrace (); One}

The printed string is identical to the data sent by the front end ...

Five, the final truth ...

I used the most primitive method to re-write an identical form, which was submitted using the form without Ajax. The background can print out the data!

Compared with two front-end HTTP request data, modified the data format of AJAX submission, resolved:

1 " application/x-www-form-urlencoded ",

That is: When an AJAX request is received, Spring MVC resolves the received request in JSON format, based on the data type indication.

But it seems name=lings&phone=13899999999 this string submitted in form data format does not match JSON parsing.

Once the data type is re-indicated, the above method of fetching is feasible.

Spring MVC cannot get the parameters and values of Ajax post

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.