jquery Ajax Example return value detailed

Source: Internet
Author: User
Tags json

In jquery, there are three ways to implement Ajax: $.ajax (), $.post, $.get ().

First we look at $.get ():

The code is as follows Copy Code
$.get ("test.jsp",
{name: ' Cssrain ', Time: ' 2008/01/21 '},//data to be passed
function (data) {
Alert ("Returned data: +");
}
)

Then look at $.post ():
Same as $.get () format.

The code is as follows Copy Code
$.post ("test.jsp",
{name: ' Cssrain ', Time: ' 2008/01/21 '},//data to be passed
function (data) {
Alert ("Returned data: +");
}
)

The difference in the above 2 ways should be the way the request is different (a get one post).


Finally we look at $.ajax ():

The code is as follows Copy Code
$.ajax ({
URL: ' accept.jsp ',
Type: ' Post ',//Data send mode
DataType: ' HTML ',//Accept data format (there are many, commonly used Html,xml,js,json)
Data: ' text= ' +$ ("#name"). Val () + ' &date= ' +new date (),//To be passed
Error:function () {//Failed
Alert (' Error loading document ');
},
Success:function (msg) {//successful
Alert ("Data Saved:" + msg);
}
});


Instance

The code for the foreground JSP section is as follows: ...
Number of votes:

The code is as follows Copy Code
<span id= "I<%=id%>" ><%=vote_number%></span><br/>
<a Onclick=myvote (<%=id%>); href= ' javascript:; ' " > Polling </a>

...
The code for the JS section is as follows

The code is as follows Copy Code

function Myvote (ID) {
$.post ("vote.jsp", {id:id},
function (data) {
Eval ("var data=" +data);
if (data.issucc== "0") {
Alert (data.mess)
}else{
Alert ("Update page");
$ ("#i" +data.myid). HTML (data.votenum);
}
});
}

Return data to JSON


The JSON data returned in the background is as follows

{issucc:,mess: "", Votenum:,myid:}

ISSUCC: Success
Mess: Information, mainly error messages, such as no login, exceeding the limit, etc.
Votenum: Total votes after the vote
myID: The ID of the poll, the number of votes used to update the page

A Registered Login instance

Js

The type returned by login.jsp is the text form and is "OK" when it is correct, when the error is
"Error".

The code is as follows Copy Code

var userName;
var password;
var result;
$ (document). Ready (function () {
$ ("#load"). Hide ();

$ ("#success"). Hide ();
$ ("#error"). Hide ();

});

$ (document). Ready (function () {
$ ("#button"). Click (function () {
$ ("#error"). Hide ();
$ ("#load"). Show ("slow");
UserName = $ ("#userName"). Val ();
Password = $ ("#password"). Val ();
$.ajax ({type: Post),
URL: "Login.jsp",
DataType: "HTML",
Data: "Username=" +username+ "&password=" +password,
Success:function (Result) {
var res = String ($.trim (result));
if (res== "OK") {
$ ("#myTable"). Hide ("slow");
$ ("#success"). Show ("slow");
}else if (res== "error") {
$ ("#error"). Show ("slow");
$ ("#load"). Hide ("slow");
}else{
Alert ("return exception");}
}
});
});
});

JSP page

The first type of responsetext format

  code is as follows copy code

<%@ page Language= "java" pageencoding= "gb2312"%> 
<% 
String userName = Request.getparameter (" UserName "); 
String password = request.getparameter (" password "); 
if (password.equals (" Longleg ") &&username.equals ("thy")) { 
    out.print ("OK"); 
}else{out.print ("error ");}  
%> 

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.