jQuery和AJAX

转自:http://blog.sina.com.cn/s/blog_6d3c1ec60101civr.html

参考网站

http://www.w3schools.com/jquery/jquery_ajax.asp

A、AJAX

AJAX = Asynchronous JavaScript and XML.

AJAX is not a new programming language, but a new way to use existing standards.

AJAX is the art of exchanging data with a server, and updating parts of a web page - without reloading the whole page.

异步JavaScript和XML。

AJAX不是一个新的程序语言,而是使用现存标准的一种新方法。

AJAX和服务器交换数据,并修改web网页的一部分,而不是重新加载整个网页。

B、jQuery实现AJAX

若不用jQuery只使用标准的JavaScript也可以实现AJAX。但是jQuery提供了更为方便的方式。

jQuery provides a rich set of methods for AJAX web development.

With jQuery AJAX, you can request text, HTML, XML, or JSON from a remote server using both HTTP Get and HTTP Post.

And you can load remote data directly into the selected HTML elements of your web page!

jQuery为AJAX Web开发提供了大量方法。

通过jQuery AJAX,你可以从远程服务器上使用HTTP Get或HTTP Post来请求text,html,xml,json等。

你可以在你的web页面上选中的HTML元素中直接加载远程服务器的数据。

B.1、load()方法

The jQuery load() method is a powerful AJAX function. It loads data from a server and puts the returned data into the selected element.

jQuery的load()方法是非常有用的AJAX函数。它从服务器上加载数据并把返回的数据放到选中的HTML元素中。

Syntax(语法)

$(selector).load(URL,data,callback);

The required URL parameter specifies the URL to send the request to.

URL:指定请求的URL。

The optional data parameter specifies data to send to the server along with the request.

data(可选):指定通过请求发送的服务器上的数据。

The optional callback parameter is the name of a function to be executed after the request is completed.

callback:指定在请求成功执行之后回调函数的名称。

<html>

<head>

<script src="jquery.js"></script>

<script>

$(document).ready(function(){

$("button").click(function(){

$("div").load('test1.txt',function(){alert("abc");});

});

});

</script>

</head>

<body>

<div>

<h2>Let AJAX change this text</h2>

</div>

<button>Change Content</button>

</body>

</html>

B.2、$.ajax()

The jQuery $.ajax() method also performs an AJAX request.

$.ajax() offers more functionality than load(), get(), and post(), but it is also more difficult to use.

jQuery的$.ajax()方法也是执行一个AJAX请求。

$.ajax()比load(), get(), post()提供更多的功能,但是它也比它们更难以使用。

Syntax(语法)

$.ajax(options);

The options parameter takes name:value pairs defining URL, passwords, data types, filters, character sets, timeout and error functions.

options:该参数以name:value成对出现,定义了URL,passwords, data types, filters, character sets, timeout和error functions。

<html>

<head>

<script src="jquery.js"></script>

<script>

$(document).ready(function(){

$("button").click(function(){

$.ajax({url:"test1.txt",success:function(result){

$("div").html(result);

}});

});

});

</script>

</head>

<body>

<div>

<h2>Let AJAX change this text</h2>

</div>

<button>Change Content</button>

</body>

</html>

B.3、其它jQuery AJAX方法

$.ajax()

Performs an AJAX request

ajaxComplete()

Specifies a function to run when the AJAX request completes

指定一个函数在完成执行AJAX请求后运行它。

ajaxError()

Specifies a function to run when the AJAX request completes with an error

指定一个函数在完成执行AJAX请求但带有错误后运行它。

ajaxSend()

Specifies a function to run before the AJAX request is sent

指定一个函数在AJAX请求发送前运行它。

$.ajaxSetup()

Sets the default values for future AJAX requests

为未来的AJAX请求设置默认值。

ajaxStart()

Specifies a function to run when the first AJAX request begins

指定一个函数当地一个AJAX请求开始时运行它。

ajaxStop()

Specifies a function to run when all AJAX requests have completed

指定一个函数当所有AJAX请求完成后运行它。

ajaxSuccess()

Specifies a function to run an AJAX request completes successfully

指定一个函数当某个AJAX请求成功执行后运行它。

$.get()

Loads data from a server using an AJAX HTTP GET request

使用AJAX HTTP GET请求来加载服务器数据。

$.getJSON()

Loads JSON-encoded data from a server using a HTTP GET request

使用HTTP GET请求来加载服务器上的JSON数据。

$.getScript()

Loads (and executes) a JavaScript from the a server using an AJAX HTTP GET request

使用HTTP GET请求加载并执行服务器上的一个JavaScript。

load()

Loads data from a server and puts the returned HTML into the selected element

$.param()

Creates a serialized representation of an array or object (can be used as URL query string for AJAX requests)

为一个数组或对象创建一个可序列化的表示法。

$.post()

Loads data from a server using an AJAX HTTP POST request

使用AJAX HTTP POST请求来加载服务器数据。

serialize()

Encodes a set of form elements as a string for submission

把一组form元素编码成string用来提交

serializeArray()

Encodes a set of form elements as an array of names and values

把一组form元素编码成name和value数组


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章