Ajax Success: description, features, operating instructions

Ajax Success is a tool for loading data from a server into a web browser without reloading the entire page. In his work, he uses a JavaScript-based XMLHttpRequest object to send and receive information on the server asynchronously, in the background, without user intervention. The method has become so popular that there is hardly any other application that does not use Ajax. An example of some large-scale online applications managed by Ajax Success are: Gmail, Google Maps, Google Docs, YouTube, Facebook, Flickr and others.

Cross browser capabilities

Script selectors and operations




Ajax stands for: A - Synchronous, Javascript, A nd X ml. As can be seen from the notation, the process uses JavaScript, or rather a script called jquery.js. jQuery is a JavaScript library. The file weighs only 96 kb, is written in JavaScript and functions with the name jquery.js, which simplifies JavaScript programming by providing cross-browser capabilities for selecting and processing DOM elements, event handling, Ajax Success request and animation.

jQuery is very popular. In May 2015, the file was used by 64.2% of all websites. Among JavaScript libraries and frameworks, jQuery's market share is 95.2%. In other words, most modern developers work in jQuery, not raw JavaScript.





JQuery benefits:

  1. Cross browser support. That is, the same jQuery code runs in different browsers: Chrome, Firefox, IE, Safari and Opera. On the other hand, to provide cross-browser support in raw JavaScript, you need to check the browser and issue the appropriate codes, since different browsers (especially IE) implement functions in different ways.
  2. Select and manipulate DOM elements. “Request” refers to the selection of DOM elements in a web document for later manipulation.
  3. Powerful selector function for selecting elements based on HTML name tags, for example, HTML attribute ID, #debug, CSS class name, .error, if using Ajax Success error.

Event handling

jQuery simplifies JavaScript event handling and has special effects and animations, adapts programming for show / hide, fade-in / fade-out, slide-in / slide-out, and custom animation. Ajax Success provides a simple Ajax interface for sending asynchronous HTTP GET / POST requests and processing the response.

Using jQuery, you can write several lines of code to replace a dozen JavaScript codes and run on all browsers without the need for separate testing on each of them. Cross-browser support is especially important for production, as the user cannot test their JavaScript codes in all browsers. Jquery Ajax Success uses more than 60% of websites on the Internet.

To work with the process, it is assumed that the user is familiar with HTML5, CSS3 and JavaScript, which are necessary prerequisites for switching to jQuery.





Using jQuery

JQuery has two versions. The second version does not support IE <9. Since there are still quite a lot of old IEs (v7, v8), the first version is much more popular on production systems.

Installation and configuration process:

  1. Download the jQuery library from the site.
  2. Copy the JavaScript file (for example, jquery-1.xx.x.min.js) to the root directory of the document, usually under the “js” subdirectory. “Min.js” is a production production mini version that removes extra spaces and comments to reduce the file size for quick downloads.
  3. For testing and learning codes use the ".js" version.
  4. Include in HTML document. In HTML4 / XHTML1.0, you must include the type = "text / javascript" attribute in the opening tag.

Alternatively, instead of serving the jquery.js server, the user can use one of the CDNs (content distribution network) for maintenance. This will save some network traffic and probably provide a faster response. In addition, loading jquery.js will be cached for reuse:

  • CDN jQuery.com;
  • Google CDN;
  • Microsoft CDN.

Some users prefer to place JavaScripts just before the end of body () instead of the section for better speed, and also load CSS before JavaScripts, since it often refers to CSS.

Script selectors and operations

The Jquery Ajax Success selector, the most important jQuery function , has special $ () syntax. It can take a tag name, an id attribute (with a # prefix), or a class name (with a dot prefix). In fact, it supports all CSS selectors.

Compared to the selector functions in JavaScript, jQuery is very simple and a class higher. It attaches an event handler that will fire after the DOM tree is built. The "" event is different from the JavaScript "" event, which cannot wait for external links, such as images, to load. These codes are placed in the section before the referenced items are created. This is a common jQuery practice:

$ (document) .ready (handler) readyonloadready ()

An example is the jQuery selector and operations.

JQuery selector example and operations




A query in jQuery refers to the selection of elements in an HTML document for subsequent manipulations.

Query in jQuery relates




For instance:

  1. $ (document) selects the current document.
  2. $ (p) selects everything.
  3. Elements (Tag-Selector).
  4. $ (# hello) and $ (# message) selects one element with the id = "hello" (ID-Selector) attribute.
  5. $ (. red) selects all elements that have the class = "red" (Class-Selector) attribute.

Actually $ () is an abbreviation (alias) for the main jQuery () function.

Ways to manage item content

The user can use function chains for “Line” and “To,” since most functions return an element that is in operation. In many methods, for example, html () jQuery uses the same method name for both getter and setter, differentiating its argument. For example, html () without an argument returns innerHTML.

The ready document is typically used in jQuery, which provides an abbreviated notation written as $ (function () {...}).

Types of content management of the selected item:

  • html () - get innerHTML;
  • html (value) - set innerHTML;
  • append (value) - add innerHTML to the end;
  • prepend (value) - add before innerHTML;
  • before (element) - add element before the current element;
  • after (element) - add element after the current element;
  • addClass (value), removeClass (value), toggleClass (value) - add, remove or switch the value of the class attribute.

JQuery creates an automatic loop function "Line?" and "to?", for example, $ ('p') selects all the elements. $ ('p'). append (...) applies append (...) to each of the selected items in an implicit loop.

The user can also use an explicit loop through .each (function () {...}) (Line? To?) If he needs to apply several operations to the selected elements. Inside .each - $ (this) denotes an element when working.

Event Handling Example

This example shows how to program an event handler for user actions. Most jQuery codes actually deal with event handlers for a set of selected elements.

Event Handling Example




The example illustrates the jQuery selector and built-in functions.

Example illustrates jQuery selector




Sequencing:

  1. Select source elements through the appropriate jQuery selector.
  2. Define an event, such as a mouse click, key type.
  3. Write an event handler and attach to the source.
  4. You can attach an event handler to JavaScript, such as click, mouseover, and submit, to a selected item using jQuery methods.
  5. You can prevent the default handler from starting by returning from .click (handler) .mouseover (handler) .submit (handler) false.

Inside the function, $ (this) refers to the current object. Although $ (p) returns multiple elements (in an array), you can use the same syntax to bind an event handler to EACH elements.

They used to put JavaScript event handlers in HTML tags. The practice nowadays is to leave them outside the HTML tags and group them in a section for a better MVC design.

AJAX request and response code

AJAX request and response code




Continuation of the process:

jquery ajax success




To test Success function data Ajax, run a script under a web server, for example, Apache:

$ (: submit) selector selects all $ (: text [name = "message"]) select

You can use $ .ajax () to send an Ajax request:

  • ajax () takes an associative array (key-value pairs) as an argument;
  • type defines the request method, for example, get or post, for Ajax post Success;
  • url indicates the URL of the action; by default, the current document;
  • Data provides a query string in the form of an associative array;
  • .done () sends back when a response is received with a status code of 200 (OK). A function with an HTTP response message is required as an argument;
  • .fail () sends back when a response is received with the status code NO 200 (OK);
  • .always () sends back after .done and .fail completes. It takes a no-arg function as an argument.

JavaScript extension basics

jQuery is a JavaScript extension that is easy to understand if the user owns JavaScript. It just needs to trace some jQuery operations using Firebug or web development tools. The jQuery API is available on the developer's site.

In jQuery, operations are placed in a handlerunder, which starts after creating the DOM tree, but before loading external resources, for example, images, which is equivalent to placing jQuery scripts immediately before the closing tag. This is more efficient than the JavaScript handler. In addition, you can use several to register several handlers, which will be executed in the order in which they were registered. JavaScript can only be used once:

$ (document) .ready (handler) onloadwindow.onload = handler.ready () window.onload = handler

Accepts an argument, which is the most often anonymous function or predefined function. The function has no arguments.

jquery ajax success




It is so often used that there is a shorthand. $ (Document) .ready (handler) $ (handler).

Iterate through selected items

The jQuery selector can select null or DOM elements. The selected elements are wrapped inside the object, since [0], [1], ..., etc.

You can iterate over each of these elements:

  1. Implicit iteration using the append () function for each of the selected items in an implicit loop.
  2. Explicit iteration through .each (function).
  3. $ (this).

Suppose that the user wants to apply a series of operations to each of the selected elements, he can use iteration over the selected elements. Takes a function as an argument, which can be either anonymous, or a predefined, or variable function. Inside the function, you can use the link to the element in progress:

.each (function) and .each () $ (this).

In addition, $ (this) refers to the jQuery object under control, it can also use this, which refers to the DOM element used. That is: $ (this) [0] === this

And it can apply jQuery methods like .append (), .html () to $ (this), but not this. On the other hand, he can apply the DOM operation this, for example, this.id.substring (0.5), which means the first five characters of the id attribute of the DOM element attribute in the process.

Writing Ajax Using jQuery

The default Ajax request is asynchronous. In other words, .ajax () will be released, the script will not wait for an answer, but will move on to the next statement so as not to lock and freeze the screen.

A user can write Ajax Success Json using raw JavaScript. However, jQuery makes it a lot easier using the $ .Ajax settings or the $ .ajax URL parameters.

These settings are the object of key-value pairs.

Commonly used keys:

The URL of the request, which can be placed outside the settings of the last form.

  1. Type - GET or POST.
  2. Ajax data Success - requests parameters (name = value pairs) and is expressed as an object, for example, {name: "peter", msg: "hello"} or the query string "name = peter & msg = hello".
  3. DataType is the expected type of response data, such as text, xml, json, script, or html.
  4. Headers - an object for the key-value pair of the request header.

Server Validation for an Ajax Success php request has the request header "X-Requested-With: XMLHttpRequest". The server program can check if an Ajax request appears through this header, for example, in PHP.

Security questions

To prevent an XSS (Cross-Site Scripting) attack, the XMLHttpRequest object can only request data from the source server serving the page.

You need to be careful when loading a script. HTML in the $ element loads data from the server and places the returned HTML inside the matched element. This is the easiest Ajax method to download data from the server.

Here is an example for requesting an Ajax Success function to an HTTP server for plain text.

In this example, we use Ajax to send a POST request asynchronously to request a text file.

Security questions




This is the text of the Ajax Request response:

This is the text of the Ajax Request response




This example runs under an HTTP server (such as Apache) because it sends an HTTP request, which will be used as a placeholder for the response text. It also contains a hyperlink to run the Ajax request (via the loadAjaxText () function).

Ajax request with POST parameters for the HTTP PHP server to receive a dynamic response.

HTTP to get a dynamic response




The client HTML code contains two "s" for input and output, respectively. It also contains a hyperlink to run the Ajax Success php request (via the JavaScript loadText () function).

Javascript function




The obtained results of the script of the process of asynchronous data exchange from the web server using JavaScript confirm that even without refreshing the page, online applications work quickly and efficiently. Web applications use a request or response model from an HTML server to produce a full page. As a result, the user receives the result after pressing the button and waiting for the server to respond, pressing again and waiting for a response. Ajax performs request / response operations that do not require users to wait for responses from the server.




All Articles