JQuery AJAX 方法总结:

JQuery中主要AJax方法: php

1、load:html

$(selector).load(url,[data],[callback]);从服务器中加载数据并将返回数据放入被选元素中。ajax

参数含义:json

url : 为但愿加载URL,能够将JQuery的选择器添加到URL参数中。浏览器

data:为请求传入的参数,为键值对缓存

callback :success(data,status,xhr)  (请求成功时才调用回调函数,失败的处理须要使用ajax函数)服务器

     data:包含调用成功时的结果内容app

    status:为调用状态,成功时返回"success",失败时返回"error"dom

    xhr:返回XMLHttpRequest对象,根据该对象可获取 readyState 和 statues异步

经过查看源码可知load 的本质是经过ajax方法来实现,默认为GET方法,若是第二个参数params为函数则用GET方法,若是为对象则为POST方法。

例:

1.function handleclick()
{
   $("#responsediv").load("server.php",function(responsTxt,statusTxt,xhr){
       if(statusTxt === "error")
            this.innerHTML="Error!";
     });

发送方式为GET:

2.function handleclick()
{
   $("#responsediv").load("server.php",{"name":"LH"},function(responsTxt,statusTxt,xhr){
       if(statusTxt === "error")
            this.innerHTML="Error!";
     });

发送方式为POST:

JQuery load函数源码以下:

jQuery.fn.load = function( url, params, callback ) {
     if ( typeof url !== "string" && _load ) {
          return _load.apply( this, arguments );
     }


     var selector, type, response,
          self = this,
          off = url.indexOf(" ");
     if ( off >= 0 ) {
          selector = jQuery.trim( url.slice( off ) );
          url = url.slice( 0, off );
     }

     // If it's a function
     if ( jQuery.isFunction( params ) ) {

          // We assume that it's the callback
          callback = params;
          params = undefined;

     // Otherwise, build a param string
     } else if ( params && typeof params === "object" ) {
          type = "POST";
     }

     // If we have elements to modify, make the request
     if ( self.length > 0 ) {
          jQuery.ajax({
               url: url,
               // if "type" variable is undefined, then "GET" method will be used
               type: type,
               dataType: "html",
               data: params
          }).done(function( responseText ) {

               // Save response for use in complete callback
               response = arguments;

               self.html( selector ?

                    // If a selector was specified, locate the right elements in a dummy div
                    // Exclude scripts to avoid IE 'Permission Denied' errors
                    jQuery("<div>").append( jQuery.parseHTML( responseText ) ).find( selector ) :

                    // Otherwise use the full result
                    responseText );

          }).complete( callback && function( jqXHR, status ) {
               self.each( callback, response || [ jqXHR.responseText, status, jqXHR ] );
          });
     }

     return this;
};

二,$.get() 和 $.post()

get:

$.get(URL,[data],[callback],[type]); 经过HTTP GET 方法从服务器上请求数据。

参数:

URL:请求页面的URL地址

data: 请求参数,这里的参数会加在http请求头url后面

callback:回调函数,回调函数的3个参数:success(data,status,xhr) (请求成功时才调用回调函数,失败的处理须要使用ajax函数)

type: 请求返回数据转化成该数据类型

post:

$.post(URL,[data],[callback],[type]);经过HTTP POST 方法从服务器上请求数据。

参数:

URL:请求页面的URL地址

data: 请求参数,这里的参数放在http报文里面。

callback:回调函数,回调函数的3个参数:success(data,status,xhr)(请求成功时才调用回调函数,失败的处理须要使用ajax函数)

type: 请求返回数据转化成该数据类型

GET和POST JQuery源码:

jQuery.each( [ "get", "post" ], function( i, method ) {
     jQuery[ method ] = function( url, data, callback, type ) {
          // shift arguments if data argument was omitted
          if ( jQuery.isFunction( data ) ) {
               type = type || callback;
               callback = data;
               data = undefined;
          }

          return jQuery.ajax({
               url: url,
               type: method,
               dataType: type,
               data: data,
               success: callback
          });
     };
});

get 和 post 区别:

请求静态页面时,get请求浏览器会将数据缓存,post不会缓存

3、getJSON和getScript

getJSON:

 $.getJSON(url,[data],[callback]); 经过HTTP GET 请求载入JSON数据。

参数:

url:请求页面地址

data:请求参数

callback:回调函数,success(data,status,xhr)(请求成功时才调用回调函数,失败的处理须要使用ajax函数)

JQuery 源码:

     getJSON: function( url, data, callback ) {
          return jQuery.get( url, data, callback, "json" );
     }

getScript:

$.getScript(url,[callback]); 经过HTTP GET请求载入js脚本,并在加载完毕以后执行这个脚本

参数:

url:请求连接

callback:回调函数,success(data,status,xhr)(请求成功时才调用回调函数,失败的处理须要使用ajax函数)

JQuery 源码:

      getScript: function( url, callback ) {
          return jQuery.get( url, undefined, callback, "script" );
     }

用getScript加载脚本的优势:

正如她的特色同样,优势很明显,那就是异步请求,在页面快速载入1KB的基本js以后,而后分段依次载入100KB的脚本,固然这是假设状况。咱们熟悉的QQ空间就是利用这样的原理,一步一步的部署运行环境的,可以减小客户端的压力,而且页面的呈现不会由于js庞大而戛然而止或者止步不前。

缺点:

加剧客户端对服务端的请求次数;

4、Ajax

$.ajax([setting]); JQuery ajax 的基础函数,从以前的JQuery 源码能够看出get,post,load本质都是调用ajax()函数来实现的。

参数所有为可选:

1.数据参数:

url:请求的页面地址,默认值为当前页面地址

type:http请求类型 GET/POST

data:键值对,来传递请求参数

async:默认为true,异步请求,false为同步请求

dataType:服务器返回数据的数据类型。若是不指定,jQuery 自动根据HTTP 包MIME信息来智能判断

  • "xml": 返回 XML 文档,可用 jQuery 处理。

  • "html": 返回纯文本 HTML 信息;包含的 script 标签会在插入 dom 时执行。

  • "script": 返回纯文本 JavaScript 代码。不会自动缓存结果。除非设置了 "cache" 参数。注意:在远程请求时(不在同一个域下),全部 POST 请求都将转为 GET 请求。(由于将使用 DOM 的 script标签来加载)

  • "json": 返回 JSON 数据 。

  • "jsonp": JSONP 格式。使用 JSONP 形式调用函数时,如 "myurl?callback=?" jQuery 将自动替换 ? 为正确的函数名,以执行回调函数。

  • "text": 返回纯文本字符串

2.回调函数:

beforeSend:发送请求以前调用,XMLHttpRequest对象为惟一参数,通常可在发送请求以前修改XMLHttpRequest对象,如添加自定义HTTP头。

error:  请求失败时调用 参数有三个:XMLHttpRequset对象,错误信息,(可选)捕获的异常对象。

dataFilter:在请求成功以后调用。传入返回的数据以及 "dataType" 参数的值。传递给 success 回调函数。

success:请求成功后的回调函数,参数有3个:根据 dataType 参数进行处理后的数据,描述状态的字符串,XMLHttpRequest对象。

complete:请求完成后回调函数 (请求成功或失败以后均调用)。参数为XMLHttpRequest 对象和一个描述请求类型的字符串。

相关文章
相关标签/搜索