初步学习AJAX

什么是AJAX?

AJAX =异步JavaScript和XML。
ajax的基础是XMLHttpRequest对象。所有现代浏览器支持XMLHttpRequest对象(IE5和IE6使用ActiveXObject对象)。
所有现代浏览器(IE7 +,火狐,Chrome,苹果Safari和Opera)有一个内置的XMLHttpRequest对象。

创建一个XMLHttpRequest对象的语法:

xhr=new XMLHttpRequest();
Internet Explorer (IE5 and IE6) 使用 ActiveX 对象:

xhr=new ActiveXObject(“Microsoft.XMLHTTP”);
创建一个满足所有浏览器的对象:
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
响应准备就绪时,指定一个函数获取响应结果与执行操作
xhr.onreadystatechange=function()
{
if (xhr.readyState4 && xhr.status200)
{
函数;(函数名(xhr))
}
}
发送一个请求到服务器
向服务端发送一个请求,XMLHttpRequest对象的open()和send()方法:

xmlhttp.open(“GET”,“ajax.php”,true);
xmlhttp.send();
POST 请求
var xhr = new XMLHttpRequest();

 `if(window.XMLHttpRequest){
        xhr = new XMLHttpRequest();
    }else if(window.ActiveXObject){
         xhr = new ActiveXObject('Microsoft.XMLHTTP');
    }else{
        alert('该浏览器不支持Ajax网络请求!');
    }`

2、 xhr.onreadystatechange = function(){
if(xhr.readyState == 4){
if(xhr.status == 200){
doResponse(xhr);
}
}
}
xhr.open(‘POST’,01-reg-ajax.php,true);
POST请求中的默认Content-Type text/plain PHP服务器会拒绝接受请求数据,
必须在请求消息发送之前进行修改!
xhr.setRequestHeader(‘Content-Type’,‘application/x-www-form-urlencoded’);

get请求:
简单 GET 请求:

xhr.open("GET","demo_get.asp",true);
xhr.send();

如果发送请求时,添加一些参数,可以把参数加到url上

xhr.open(“GET”,“demo_get2.asp?fname=Henry&lname=Ford”,true);
xhr.send();

在php中接受响应
通用:$k = $_REQUEST[‘html中的’];
一定要和HTML传输过来的变量名字一样。
PHP中的数据引用
require(‘data.php’);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值