1.Ajax 

2.load()

3.$.get()$.post()

4.$.getScript()$.getJSON()

5.$.ajax()

6.

AjaAsynchronouJavaScript and XML”(步 JavaScript 和 XML, 它并不是 JavaScript 形 成的结合体。使用 Ajax户 体验。

Aja

Ajax 由 Jesse James Garrett 在 200串 技术的集合,主要有:

1.JavaScript

2.XMLHttpRequest 务 器发送请求;

3.以 XMLHTML 或 JSON 

4.它 JavaScript如 PHP 从 MySQL 其 呈现到页面上。

 

于 Ajax 

1.启 JavaScript 

2.

3.升 We

4.户端

 

而 Ajax 

1.度 XMLHttpRequest 不足(如 IE)

2.退为 Ajax 页面

3.解 JS 

4.说,JS 或 Ajax 

使用 Ajax 

的 We下 一段,类似电话中的通话,一个电话接完才能接听下个电话;而异步可以同时执行多条任务, 感觉有多条线路,类似于短信,不会因为看一条短信而停止接受另一条短信。Ajax 以 使用同步模式执行,但同步的模式属于阻塞模式,这样会导致多条线路执行时又必须一条一 条执行,会让 WeAjax 

load()

jQuery 对 Ajax 使便容 性。对于封装的方式,jQuery $.ajax()这 层封装了第二层有三种方法:.load()$.get()$.post()$.getScript()$.getJSON() 

.load()url(求 html 的 url 为 String)、 data(的 key/valu为 Object)callback(调 函数,参数类型为函数 Function)

让 Ajax 段 HTML 个 HTML 的 url 

//HTML

<inputype="button" value="/>

<div id="box"></div>

//jQuery

$('input').click(function () {

$('#box').load('test.html');

});

的 HTML 在 url 

//的 url

$('input').click(function () {

$('#box').load('test.html .my');

});

.php数据那 么我们就可以使用第二个可选参数 datage和 post

//递 data认 ge

$('input').click(function () {

$('#box').load('test.php?url=ycku');

});

//ge的 PHP

<?php

i($_GET['url'] == 'ycku') {

ech'城 We';

else {

ech'';

}

?>

//递 data为 post 

$('input').click(function () {

$('#box').load('test.php', {

url : 'ycku'

});

});

//post 的 PHP

<?php

i($_POST['url'] == 'ycku') {

ech'城 We';

else {

ech'';

}

?>

在 Ajax 数 callback

responseText

XMLHttpRequest 

$('input').click(function () {

$('#box').load('test.php', {

url : 'ycku'

textStatus(请)XMLHttpRequest

}, function (response, status, xhr) {

alert('+ response + '为:+ status + '+ xhr.statusText);

});

});

status successerrorXMLHttpRequest

于 JavaScript 

 

responseText

 

responseXML

"text/xml""application/xml"

的 XML DOM 

status

的 HTT

 

statusText

HTTP  

么 xh的 statusText 'OK'符串'OK'

statusText 

HTTP 

200

OK

400

BaRequest

401

Unauthorized

 

404

 

Not found

的 UR

 

500

 

Internal ServerError

 

503

 

ServiceUnavailable

 

$.get()$.post()

.load()的 jQuery $.get()

$.post().load(), 而对于需要传递参数到服务器页面的,$.get()$.post()

$.get()数和.load()数 type务 器返回的内容格式:包括 xmlhtmlscriptjsonjson和 text, 后面三个为可选参数。

//使$.get()回 html 

$('input').click(function () {

$.get('test.php', {

url : 'ycku'

}, function (response, status, xhr) {

i(status == 'success') {

$('#box').html(response);

});

}

})                                                                   //typ为 html

数 typ下 typ

 

 

//使$.get()回 xml

$('input').click(function () {

$.get('test.xml', function (response, status, xhr) {

$('#box').html($(response).find('root').find('url').text());

});                                                                  //typ为 xml

});

是 xml 件,typ置 html 会 把 xml 照 xml 

//使$.get()回 json

$.get('test.json', function (response, status, xhr) {

});

alert(response[0].url);

$.post()使$.get()一致

使

1.GE过 UR而 POS是 HTTP 

2.GE2KB而 POS

3.GE而 POS

4.GE$_GET[]POS$_POST[]

//使$.post()回 html

$.post('test.php', {

url : 'ycku'

}, function (response, status, xhr) {

$('#box').html(response);

});

$.getScript()$.getJSON()

jQuery $.getScript()的 JS 

$.getJSON()载 JSON 

载 JS 有 JS , 这时课时使用$.getScript()

//载 JS 

$('input').click(function () {

$.getScript('test.js');

});

$.getJSON()载 JSON 使

$('input').click(function () {

$.getJSON('test.json', function (response, status, xhr) {

alert(response[0].url);

});

});

$.ajax()

$.ajax()有 aja$.ajax()。 这个方法只有一个参数,传递一个各个功能键值对的对象。

$.ajax()

url

String

type

String

POS或 GET认 GET  


timeout

Number

 

data

Object

String

 

 

dataType

 

String

如 htmlxmljso

 

beforeSend

 

Function

改 XMLHttpRequest 

 

complete

 

Function

 

success

 

Function

 

error

 

Function

 

global

 

Boolean

为 true局 Ajax

 

cache

 

Boolean

浏览缓存为 truedataType

为 script 或 json为 false

 

content

 

DOM

指定某元素为与这个求相关的所有

 

contentType

 

String

指 定 请 求 内 容 的 类 型 。 默 认 为

application/x-www-form-urlencoded

 

async

 

Boolean

为 truefalse 

 

 

processData

 

 

Boolean

默认为 true,数据被处为 UR编码格式。

为 false将传据处为 URL编 码的格式。

 

dataFilter

 

Function

 

 

ifModified

 

 

Boolean

为 false行头为 true

被认为是成功的。

 

jsonp

 

String

指定一查询参数名称覆盖默认的 json

名 callback

 

username

 

String

在 HTTP 使

 

password

 

String

在 HTTP 使

 

scriptCharset

 

String

使

置 script 和 json使

 

xhr

 

Function

供 XHR 

 

traditional

 

Boolean

为 false使风格

为 true使

 

//$.ajax 使

$('input').click(function () {

$.ajax({

typ: 'POST',                                                  //成 GET

url : 'test.php', data : {

url : 'ycku'

},

success : function (response, stutas, xhr) {

$('#box').html(response);

});

}

});

于 dat,如是 GE使形式

POS使

表单序

Ajax 过 submit 传 输到服务器端。如果使用 Ajax 提 交。这样工作效率就大大降低。

//

$('form input[type=button]').click(function () {

$.ajax({

typ: 'POST', url : 'test.php',data : {

user : $('form input[name=user]').val(),

email : $('form input[name=email]').val()

},

success : function (response, status, xhr) {

alert(response);

});

}

});

使.serialize()

使用 Ajax 

//使.serialize()

$('form input[type=button]').click(function () {

$.ajax({

typ: 'POST', url : 'test.php',

dat: $('form').serialize(),

success : function (response, status, xhr) {

alert(response);

}

});

});

.serialize()拉 列表框等内容。

//使

$(':radio').click(function () {

$('#box').html(decodeURIComponent($(this).serialize()));

});

.serialize()回 JSON 方法.serializeArray()法 可以直接把数据整合成键值对的 JSON 

$(':radio').click(function () {console.log($(this).serializeArray()); var json =$(this).serializeArray();

$('#box').html(json[0].value);

});

$.ajax()这 个时候我们课时使用 jQuery 供的$.ajaxSetup()

$('form input[type=button]').click(function () {

$.ajaxSetup({

typ: 'POST', url : 'test.php',

dat: $('form').serialize()

});

$.ajax({

success : function (response, status, xhr) {

alert(response);

});

}

});

使用 dat使$.param()

vaobj = {a : 1, b : 2, c : 3}; var form =$.param(obj); alert(form);

使$.param()为 UR稳 定准确的传递表单内容。因为有时程序对于复杂的序列化解析能力有限,所以直接传递 ob

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐