Asynchronous submission of forms with SPRINGMVC and jquery Ajax

Source: Internet
Author: User
Tags html header

Because of the use of jquery, we need to introduce the JS file of jquery

In the HTML header

<script type= "Text/javascript" src= "./js/jquery.min.js" ></script>

The specific directory location is changed according to your project.

The HTML for the front end is as follows: "To make the code look clearer, I've cleared out all the styles."

<!--login form--><label> user name </label><input id= "username"id= "password" id= " Login-button "> Login </button>

public.js"Asynchronous Commit I write in this file"

$(function () {     //When the login button is clicked,submitting a form asynchronously$ (' #login-button '). Click (function () {
Gets the user name and password entered by the uservarUSM = $ (' #username '). Val (); varPSW = $ (' #password '). Val (); if(USM = = "" | | PSW = = "") {alert ("The user name or password cannot be empty!"); } Else { //submitting a form asynchronously$.ajax ({ data: { "username": USM, "password" : PSW}, Type: "POST" ,URL: "/login" , success:function(flag) {//receiving the server's data}, Error:function(flag) {//Error Handling } }); } });});

SPRINGMVC Backend Controller:

@ResponseBody    produces = "text/html;charset=utf-8;")    username password)  {        //data is a JSON object, similar to a map, which can be placed into a key-value pair and uploaded to the front-end        jsonobject data = new Jsonobject ();     System.out.println ("Username:" +username+ "," +password ");        Data.put ("state", true);        return data.tostring ();    }

Note: The Jsonobject class is used here, and the Maven dependency for this class is:

<!--JSON Support--
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>
At the same time, you need to add the Spring-servlet.xml
<mvc:annotation-driven/>
Support for JSON

If you have any errors, please comment on my corrections.

Asynchronous submission of forms with SPRINGMVC and jquery Ajax

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.