Ajax sends requests to the server and returns JSON data

Source: Internet
Author: User

Here's what you want to do: when registering, the user name is already registered. When the user loses focus, the use of Ajax to send requests to the server side, the server side of the database query, if there is the same user name, the page prompts the user name has been registered, can not be used, or by registration.

Here is the jquery Ajax and the SSH framework, here's my implementation steps:

First introduce the Struts-json jar package

1. Add JS method call to the registration page

<span id= "Tip" style= "color:red" ></span><s:form action= "Register" ><s:textfield id= "username" Name= "username" label= "user name" onblur= "Validatename ()"/><s:password name= "password" label= "password"/><s:submit value= "Register"/></s:form>
The jquery method is as follows: where the URL is the requested address, type is the HTTP request method, data is the parameter sent to the server, datatype is the returned data type, success is the callback function after the request succeeds, If you want to set a callback whether the request succeeds or not, it can be set to complete

<script language= "JavaScript" >function validatename () {var user= "username=" +$ (' #username '). Val (); $.ajax ({URL : "Validatename.action", type: ' Post ', Data:user,datatype: ' Text ', Success:processresponse});} function ProcessResponse (msg) {var data = eval ("(" + msg+ ")"); $ ("#tip"). HTML (data.tip);} </script>

In this case, here the datatype return type is text, the callback function can be used to parse the JSON data, using datatype: ' JSON ' to return an object, with Eval will report JS error


2. Next write the Action class

private string Username;private string Password;private string tip;private loginservice ls;public string execute () {try {i F (Ls.validatename (GetUserName ())) {Settip ("username you entered" + getusername () + "has been registered!) ");} else {settip ("Your user name" + getusername () + "is available!) ");}} catch (Exception e) {Settip (E.getmessage ());} return SUCCESS;}


3, persistent Layer Logindaoimpl class to add a method to verify the user name, the other with the previous blog write changes

public boolean validatename (String name) {try {list list= (list) gethibernatetemplate (). Find ("from Login a where A.username =? ", name); if (List!=null && list.size () >0) {return true;} Else{return false;}} catch (Exception e) {e.printstacktrace (); return false;}}
Here to specifically explain, from the back of the login is our entity class, not the table name, I began to write the login (table name), debugging found when executed to this sentence is said Tomcat login No mapping error

4, configuration files in this will not say, the key to the struts configuration, need to inherit the Json-default package, do some small changes, instead:

Extends= "Json-default,struts-default"
<action name= "Validatename" class= "validatenameaction" ><result type= "JSON" ></result></action >
The result type is set to JSON


5, test, enter the database already existing user name, page prompt user name already exists, do not register





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Ajax sends requests to the server and returns JSON data

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.