Detailed explanation of 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. .Follow-up 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 ); } });
Instance
The code of the front-end jsp part is as follows:...
Number of votes:
The code is as follows:
<span id="i<%=id%>"><%=vote_number%></span><br/> <a onclick =myvote(<%=id%>); href=' javascript :;'">投票</a>
...
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); } }); }
Return data is json
Returned in the background The json data is as follows
{issucc:,mess:"",votenum:,myid:}
issucc: Whether it was 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 The type returned is In text format, 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 Detailed explanation of return value of jquery ajax example. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Build an autocomplete suggestion engine using PHP and Ajax: Server-side script: handles Ajax requests and returns suggestions (autocomplete.php). Client script: Send Ajax request and display suggestions (autocomplete.js). Practical case: Include script in HTML page and specify search-input element identifier.

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

Ajax (Asynchronous JavaScript and XML) allows adding dynamic content without reloading the page. Using PHP and Ajax, you can dynamically load a product list: HTML creates a page with a container element, and the Ajax request adds the data to that element after loading it. JavaScript uses Ajax to send a request to the server through XMLHttpRequest to obtain product data in JSON format from the server. PHP uses MySQL to query product data from the database and encode it into JSON format. JavaScript parses the JSON data and displays it in the page container. Clicking the button triggers an Ajax request to load the product list.
