加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Ajax数据请求

发布时间:2020-12-16 01:45:45 所属栏目:百科 来源:网络整理
导读:学 习 要 点 : 1 . Aj ax 概 述 2 . l oad () 方 法 3 . $ .ge t() 和 $ .po st() 4 . $ .ge tScript() 和 $ .ge tJSON() 5 . $ .a jax() 方 法 6 . 表 单 序 列 化 Aj a x 全 称 为 : “ A sy n c h r o no u s J v S ri ptand X ML ”( 异 步 ri pt 和

1.Ajax

2.load()

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

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

5.$.ajax()

6.

AjaxAsynchronousJvSriptandXML”(ript,它并不是形成的结合体。使用ax户体验。

Ajax

AjaxJesseJamesGarrett2005串技术的集合,主要有:

1.JavaScript

2XMLHttpRequest务器发送请求;

3XMLHTMLJSON4PHPMySQL其呈现到页面上。

Web户端

不足(IE)退页面JS说,

使

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

jQuery便容性。对于封装的方式,$.ajax()这层封装了第二层有三种方法:.load().get().post().getScript().getJSON()rl(tmlrlring)dakey/Object)callback调函数,参数类型为函数Function//HTML

<inputtype="button"value=""/>

<divid="box"></div>

//jQuery

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

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

});

rl

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

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

});

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

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

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

});

HP

<?php

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

echo'Web';

}else{

echo'';

}

?>

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

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

url:'ycku'

});

});

HP

<?php

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

echo'Web';

}else{

echo'';

}

?>

back

responseText

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

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

url:'ycku'

ex(请XMLHttpRequest

},function(response,status,xhr) {

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

});

});

statussuccessrrorXMLHttpRequest

responseText

responseXML

"t/"apli

XMLDOM

status

HTTP

statusText

HTTP

xhstatusText'OK'符串

200

OK

40BdRequest

1

Unauthorized

4

Notfound

URL

500

InternalServerError

3

rviceUnavailable

.get().post()

.post(),而对于需要传递参数到服务器页面的,数和ype务器返回的内容格式:包括scriptsonsonp,后面三个为可选参数。

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

$.get('test.php',{

url:'ycku'

},xhr) {

if(status=='success'){

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

});

}

}) //typml

ml

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

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

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

}); ml

});

件,会 把son

$.get('test.json',xhr){

});

alert(response[0].url);

.get()一致

GETOSTKB$_GET[]OST[]ml

$.post('test.php',xhr) {

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

});

.getScript().getJSON()

,这时课时使用

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

$.getScript('test.js');

});

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

$.getJSON('test.json',xhr){

alert(response[0].url);

});

});

.ajax()

。这个方法只有一个参数,传递一个各个功能键值对的对象。

rl

ig

e

GET


meout

Number

a

Object

g

Te

son

beforeSend

n

complete

success

rror

gobal

ooearueAjax

cach浏览缓存e

false

conent

DOM

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

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

app-www-form-urlencodedsync

rocessData

默认rue,数据被处编码格式。

将传据处编码的格式。

r

fModified

行头

被认为是成功的。

sonp

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

sername

password

scriptCharset

XHRraditional

风格

.ajax

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

$.ajax({

type:'POST',GET

url:'test.php',data:{

url:'ycku'

},

success:function(response,stutas,xhr){

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

});

}

});

,如形式

表单序

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

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

$.ajax({

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

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

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

},xhr){

alert(response);

});

}

});

.serialize()

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

$.ajax({

type:'POST',

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

success:function(response,xhr){

alert(response);

}

});

});

拉列表框等内容。

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

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

});

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

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

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

});

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

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

$.ajaxSetup({

type:'POST',

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

});

$.ajax({

success:function(response,51)">候.param()

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读