Home > Article > Web Front-end > Explain the return value of jquery ajax example
The main difference between $.ajax() and ($.post(), $.get()) is that after a successful callback, the execution of success. $.post(), $.get() can only be simple Pass it and return. Subsequent work cannot continue. So depending on the situation, call
In JQuery, there are three ways to implement AJAX: $.ajax(), $.post, $.get().
First we look at $.get():
The code is as follows:
$.get("test.jsp", { name: "cssrain", time: "2008/01/21" }, //要传递的数据 function(data){ alert("返回的数据: " + data); } )
Then we look at $.post():
The format is the same as $.get() .
The code is as follows:
$.post("test.jsp", { name: "cssrain", time: "2008/01/21" }, //要传递的数据 function(data){ alert("返回的数据: " + data); } )
The difference between the above two methods should be the different request methods (one get and one post).
Finally we look at $.ajax():
The code is as follows:
$.ajax({ url:'Accept.jsp', type:'post', //数据发送方式 dataType:'html', //接受数据格式 (这里有很多,常用的有html,xml,js,json) data:'text='+$("#name").val()+'&date='+new Date(), //要传递的数据 error: function(){ //失败 alert('Error loading document '); }, success: function(msg){ //成功 alert( "Data Saved: " + msg ); } });
Example
The code of the front-end jsp part is as follows:...
Number of votes:
The code is as follows:
1b2efd02bed8532fc096b8eb80f065d8">38df51ccd380352889eb6eff37977be754bdf357c58b8a65c66d7c19c8e4d114076402276aae5dbec7f672f8f4e5cc81
e6fdd16d11a6bacb6f9ffd6fbcd73ea7); href='javascript:;'">Vote5db79b134e9f6b82c0b36e0489ee08ed
...
The code for the js part is as follows
The code is as follows:
function myvote(id){ $.post("vote.jsp", { id: id }, function(data){ eval("var data="+data); if (data.issucc=="0"){ alert(data.mess) }else{ //alert(" 更新 页面"); $("#i"+data.myid).html(data.votenum); } }); }
The return data is json
The json data returned by the background As follows
{issucc:,mess:”“,votenum:,myid:}
issucc: Whether it is successful
mess: Information, mainly error message, such as not logged in, exceeding the limit etc.
votenum: the total number of votes after voting
myid: the id of the vote, the number of votes used to update the page
A registered login instance
js
login.jsp returns the type in text form , it is "OK" when it is correct, and it is
"error" when it is wrong.
The code is as follows:
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("返回异常");} } }); }); });
jsp page
The first responseText format
The code is as follows:
<%@ 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");} %>
The above is the detailed content of Explain the return value of jquery ajax example. For more information, please follow other related articles on the PHP Chinese website!