//创建XMLHttpRequest对象
var xhr;
if (window.XMLHttpRequest) {
// code for IE7+、Firefor、Chrome、Opera、Safari
xhr = new XMLHttpRequest();
} else {
// code for IE6、IE5
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
//向服务器发送GET请求, GET在发送请求是有时候是会有缓存的,所以需要加上随机数,true 代表的是异步交互, send 发送
xhr.open("GET", "www.baidu.com?t=" + Math.random(), true);
//发送请求
xhr.send();
//向服务器发送POST请求,
xhr.open("POST", "www.baidu.com", true);
//在发送之前需要设置头信息(HTTP协议),通过set设置头信息(setRequestHeader),参数二为设置编码格式为
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//发送请求,send()方法中规定您希望发送的数据
xhr.send("fname=Bill&lname=Gates");
/*
发送时选择GET还是POST ?
1、与POST相比,GET更简单也更快,并且在大部分情况下都能使用
什么情况下使用POST请求
1、无法使用缓存文件(更新服务器的文件或数据库) add
2、向服务器发送大量数据(POST没有数据量限制)
3、发送包含未知字符的用户输入时,POST比GET更稳当也更靠谱
XMLHttpRequest对象的三个重要的属性:
1、readyState属性:存有XMLHttpRequest的状态信息,从0到4发生的变化。
0:请求为初始化
1:服务器连接已建立
2:请求已接受
3:请求处理中
4:请求已完成,且响应已就绪
2、onreadystatechange属性:每当readyState改变时,就会触发onreadystatechange事件
3、Status状态属性:
200:" ok "
404:未找到页面
*/
//完成请求成功代码
xhr.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
//执行成功,this.responseText 或者 this.responseXML
//onreadystatechange事件被触发5次(0-4),对应着readyState的每个变化
}
}
/*
PHP请求简单完整请求代码:
<!--
$xxx: 什么变量
$name: 获取XMLHTTP请求参数
$dsn: 数据库信息
$db: 连接数据库 参数1、数据库地址及数据库名称 参数2、数据用户名 参数3、数据库密码
$_res: query ”执行sql语句“ 增删改查
fetch():fetch函数表示查询到了
-->
<?php
$name =$_POST['username'];
$dsn = "mysql:host=localhost;dbname=zhangsan";
$db = new PDO($dsn,'root','');
$_res =$db->query("select * from 表名 where 列名='" $name "'");
if($res ->fetch()){
echo 0;
}else{
echo 1;
}
Javascript XMLHttpRequest请求简单代码
var name = "张三";
var xhr;
if(windw.XMLHttpRequest){
xhr = new XMLHttpRequest();
}else{
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
//GET请求方式一
// xhr.open("get","www.baidu.com?t="+Math.random(),ture);
// xhr.send();
//POST请求方式一
xhr.open("post","www.baidu.com",ture);
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhr.send("username="+name);
xhr.onreadystatechange = function(){
if(this.readyState ==4 && this.Status ==200){
//请求成功后接受返回数据
if(this.responseText !=1){
alert("不可以注册")
}else{
alert("可以注册");
}
}
}
var xhr;
if (window.XMLHttpRequest) {
// code for IE7+、Firefor、Chrome、Opera、Safari
xhr = new XMLHttpRequest();
} else {
// code for IE6、IE5
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
//向服务器发送GET请求, GET在发送请求是有时候是会有缓存的,所以需要加上随机数,true 代表的是异步交互, send 发送
xhr.open("GET", "www.baidu.com?t=" + Math.random(), true);
//发送请求
xhr.send();
//向服务器发送POST请求,
xhr.open("POST", "www.baidu.com", true);
//在发送之前需要设置头信息(HTTP协议),通过set设置头信息(setRequestHeader),参数二为设置编码格式为
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//发送请求,send()方法中规定您希望发送的数据
xhr.send("fname=Bill&lname=Gates");
/*
发送时选择GET还是POST ?
1、与POST相比,GET更简单也更快,并且在大部分情况下都能使用
什么情况下使用POST请求
1、无法使用缓存文件(更新服务器的文件或数据库) add
2、向服务器发送大量数据(POST没有数据量限制)
3、发送包含未知字符的用户输入时,POST比GET更稳当也更靠谱
XMLHttpRequest对象的三个重要的属性:
1、readyState属性:存有XMLHttpRequest的状态信息,从0到4发生的变化。
0:请求为初始化
1:服务器连接已建立
2:请求已接受
3:请求处理中
4:请求已完成,且响应已就绪
2、onreadystatechange属性:每当readyState改变时,就会触发onreadystatechange事件
3、Status状态属性:
200:" ok "
404:未找到页面
*/
//完成请求成功代码
xhr.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
//执行成功,this.responseText 或者 this.responseXML
//onreadystatechange事件被触发5次(0-4),对应着readyState的每个变化
}
}
/*
PHP请求简单完整请求代码:
<!--
$xxx: 什么变量
$name: 获取XMLHTTP请求参数
$dsn: 数据库信息
$db: 连接数据库 参数1、数据库地址及数据库名称 参数2、数据用户名 参数3、数据库密码
$_res: query ”执行sql语句“ 增删改查
fetch():fetch函数表示查询到了
-->
<?php
$name =$_POST['username'];
$dsn = "mysql:host=localhost;dbname=zhangsan";
$db = new PDO($dsn,'root','');
$_res =$db->query("select * from 表名 where 列名='" $name "'");
if($res ->fetch()){
echo 0;
}else{
echo 1;
}
Javascript XMLHttpRequest请求简单代码
var name = "张三";
var xhr;
if(windw.XMLHttpRequest){
xhr = new XMLHttpRequest();
}else{
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
//GET请求方式一
// xhr.open("get","www.baidu.com?t="+Math.random(),ture);
// xhr.send();
//POST请求方式一
xhr.open("post","www.baidu.com",ture);
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhr.send("username="+name);
xhr.onreadystatechange = function(){
if(this.readyState ==4 && this.Status ==200){
//请求成功后接受返回数据
if(this.responseText !=1){
alert("不可以注册")
}else{
alert("可以注册");
}
}
}
*/