How to operate AJAX in the jQuery library of JavaScript _ jquery

Source: Internet
Author: User
Tags echo date
This article mainly introduces how to operate AJAX in the jQuery library of JavaScript, including using jQuery to simplify Ajax development. For more information, see Java software development, in the background, we can encapsulate code through various frameworks, such as SSH, to facilitate the compilation of Java code, such as Struts, springMVC encapsulates and controls the process from the front-end to the action, so that we only need some simple configuration to implement it. Spring encapsulates the management of various objects, the AOP programming method is provided, which greatly facilitates us. Hibernate and IBatis encapsulate JDBC code and do not need to write repeated and complicated JDBC code every time.

At the front-end, we have completed some page effects and verification through the JavaScript language, but it is like our Java code and is the most basic of the front-end language, JQuery encapsulates js Code to facilitate the compilation of front-end Code. In addition, it also has a major advantage in solving browser compatibility issues, this is one of the most important reasons for using it.

Now, to meet users' needs, Asynchronous refreshing of Ajax (Asynchronous Javascript + XML) has played an incomparable role. Previously, Ajax operations were written, we always need to perform several necessary steps like the JDBC code:
AJAX-core XMLHttpRequest object, while JQuery also encapsulates Ajax asynchronous operations. Here we will look at several common methods. $. Ajax, $. post, $. get, $. getJSON.

$. Ajax: This is the most basic step for JQuery to encapsulate ajax. You can use this function to complete all functions of asynchronous communication. In other words, we can use this method to perform asynchronous refresh operations under any circumstances. However, it has many parameters and may be troublesome sometimes. Take a look at common parameters:

Var configObj = {method // data submission method: get and post url // data submission method async // does it support asynchronous refresh, the default value is true data // the type of data that needs to be submitted by the ype // type of data returned by the server, such as xml, String, json and other success // callback function error after successful request // callback function after failed request} $. ajax (configObj); // use $. call ajax functions.


Let's take a look at the actual example. Let's look at an example of asynchronous deletion:

In Java software development, we can encapsulate code through various frameworks, such as SSH, in the background to facilitate the compilation of Java code, such as Struts, springMVC encapsulates and controls the process from the front-end to the action, so that we only need some simple configuration to implement it. Spring encapsulates the management of various objects, the AOP programming method is provided, which greatly facilitates us. Hibernate and IBatis encapsulate JDBC code and do not need to write repeated and complicated JDBC code every time.

At the front-end, we have completed some page effects and verification through the JavaScript language, but it is like our Java code and is the most basic of the front-end language, JQuery encapsulates js Code to facilitate the compilation of front-end Code. In addition, it also has a major advantage in solving browser compatibility issues, this is one of the most important reasons for using it.

Now, to meet users' needs, Asynchronous refreshing of Ajax (Asynchronous Javascript + XML) has played an incomparable role. Previously, Ajax operations were written, we always need to perform several necessary steps like the JDBC code:
AJAX-core XMLHttpRequest object, while JQuery also encapsulates Ajax asynchronous operations. Here we will look at several common methods. $. Ajax, $. post, $. get, $. getJSON.

$. Ajax: This is the most basic step for JQuery to encapsulate ajax. You can use this function to complete all functions of asynchronous communication. In other words, we can use this method to perform asynchronous refresh operations under any circumstances. However, it has many parameters and may be troublesome sometimes. Take a look at common parameters:

Var configObj = {method // data submission method: get and post url // data submission method async // does it support asynchronous refresh, the default value is true data // the type of data that needs to be submitted by the ype // type of data returned by the server, such as xml, String, json and other success // callback function error after successful request // callback function after failed request} $. ajax (configObj); // use $. call ajax functions.


Let's take a look at the actual example. Let's look at an example of asynchronous deletion:

// Delete $. ajax ({type: "POST", // submission url: "$ {pageContext. request. contextPath}/org/doDelete. action ", // path data: {" org. id ":" $ {org. id} "}, // data, which is transmitted in Json format success: function (result) {// The returned data is processed accordingly if (result. success) {$ ("# tipMsg "). text ("data deleted"); tree. deleteItem ("$ {org. id} ", true);} else {$ (" # tipMsg "). text ("failed to delete data ");}}});

$. Post, this function is actually further encapsulated for $. ajax, reducing parameters and simplifying operations, but the scope of application is smaller. $. Post simplifies the data submission method and can only be submitted in POST mode. It can only be an asynchronous access server. It cannot be accessed synchronously and cannot handle errors. When these conditions are met, we can use this function to facilitate our programming. Its main parameters, such as method and async, are set by default and cannot be changed. The example is not described.
Url: The sending request address.
Data: Key/value parameter to be sent.
Callback: callback function when sending successfully.
Type: The returned content format is xml, html, script, json, text, and _ default.

$. Get, and $. like post, this function encapsulates the submitted data of the get method. It can only be used in the way that the get data is submitted to solve asynchronous refresh. The usage is similar to the above. We will not demonstrate it here.

$. GetJSON, which is further encapsulated, that is, operations on the returned data type as Json. There are three parameters in it, which need to be set, which is very simple: url, [data], [callback].

In fact, the $. ajax method will be used in other cases. It is the same and actually very simple.

However, there is another problem, which is troublesome. What should I do if the page data volume is large? In conventional form processing, we can use the framework Struts2 to automatically obtain and encapsulate the form in the domain-driven mode. How can we encapsulate the form through ajax? Here, JQuery has a plug-in, Jquery Form. By introducing this js file, we can imitate the Form to support the Struts2 domain-Driven Mode for Automatic Data encapsulation. Usage is similar to $. ajax. Let's take a look at the actual example. Here we write a front-end code to save the user:

$ (Function () {var options = {beforeSubmit: function () {// process the previously required functions $ ("tipMsg "). text ("data is being saved. Please wait... "); $ (" # insertBtn "). attr ("disabled", true) ;}, success: function (result) {// The callback function if (result. success) {$ ("# tipMsg "). text ("organization saved successfully"); // This is the corresponding tree. We will introduce it later. // control the tree component and add a new node var tree = window. parent. treeFrame. tree; tree. insertNewChild ("$ {org. id} ", result. id, result. name);} else {$ ("# tipMsg "). text ("failed to save organizations");} // enable the Save button $ ("# insertBtn "). attr ("disabled", false) ;}, clearForm: true };$ ('# orgForm '). ajaxForm (options); // use Jquery. submitting the ajaxForm method in Form });


In this way, we no longer need to encapsulate and process data, which greatly simplifies asynchronous refresh operations like ajax operations. In summary, the ajax operations in JQuery are much used, which is very similar to form processing, but the functions are different. Learning programming is actually to learn how to process data flow, how to obtain data from the front-end, transfer data to the server for corresponding processing, and then return data for relevant display, it is very interesting to have completed software development through some technical implementation of this process.

Use jQuery to simplify Ajax Development

Ajax is not a language. It is only a combination of several old technologies, such as JavaScript, XML, and DOM. It is called Asynchronous JavaScript and XML, that is, Asynchronous JavaScript and XML, it can perform asynchronous data interaction with the server without refreshing the page. If used properly, it can improve user experience, save bandwidth, and reduce server pressure. As an excellent JavaScript framework, jQuery also provides many methods to handle Ajax-related operations.
Use Ajax to obtain asynchronous data

The general steps of traditional Ajax are as follows:

  • Create XMLHttpRequest (compatible with various browsers ).
  • Send an XMLHttpRequest request to the server.
  • Monitors the onreadystatechange event of XMLHttpRequest.
  • Determine whether the server completes the response and whether the response result is normal.
  • Obtain the results returned by Ajax from the server.

For example, you can use Ajax to obtain the current time on the server. The sample code is as follows:

Traditional 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.