jquery Learning Notes Ajax Operational Chapter (iii)-Process processing _jquery

Source: Internet
Author: User
Tags error handling

observation function

The Ajaxstart and Ajaxstop functions can be used as observation functions, and we can use the callback function of the observation function to do the corresponding processing.

The Ajaxstart callback function is triggered when an AJAX request begins and no other transfer has been made.
When the last active request terminates, a callback function registered through Ajaxstop is executed.
Because the observation function is global, it needs to be invoked using $ (document). We tested two functions by using an Ajax method to get an example of a picture:
The current page is:

<div></div>
<button>load</button>

The test.html content under the same directory is:

 
 

When you click the button, you want to load the image:

 $ (' button '). Click (function () {
  $ (' div '). Load (' test.html ');
 });

At this point we can use the Ajaxstart and Ajaxstop functions to add hints:

 $ (document). Ajaxstart (function () {//
  alert (' Load a picture ');
 }). Ajaxstop (function () {
  alert (' Show a picture ');
 });
 $ (' button '). Click (function () {
  $ (' div '). Load (' test.html ');
 });

Click on the button at this time, then the image load before the first hint load a picture, load prompted show a pictures.

Error handling

The most common way is the global Ajaxerror method, for example, if we send a data request like a nonexistent page:

 $ (document). Ajaxerror (function () {//
  alert (' Load failed! ');
 });
 $ (' button '). Click (function () {
  $ (' div '). Load (' noexsited.html ');
 });

Click the button at this point:

For non-load methods, you can also use the Fail method to consonant processing:

 $ (' button '). Click (function () {
  $.get (' noexsited.html ', function (data) {

  }). Fail (function (JQXHR) {
   Alert (' status is ' + Jqxhr.status);};});
 

JSONP

JSONP is json with padding, filled json, using the idea that <script> tags can get Javascript files across domains, so you can get JSON data across domains.
The format of the JSONP is to wrap the standard JSON file in a pair of parentheses, and the parentheses are placed in front of an arbitrary string. This string, which is called P, is determined by the client requesting the data.
The same is the case for the button, first we set the otherdomain.com/index.php content of the Outland page to:

<?php
$data = ' {' name ': ' Stephenlee ', ' sex ': ' Male '} ';
Echo $_get[' callback ']. ' ('. $data. ') ';

We use special placeholders? To implement Cross-domain fetch of JSON data:

 $ (' button '). Click (function () {
  $.getjson (' otherdomain.com/index.php?callback=? '), function (data) {
   Console.log (data);});
 


Data was successfully obtained.

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.