前言:
時間順流而下,生活逆水行舟
--------------------------------正文---------------------------------
替換:str.replace('把誰替換','替換成誰');
例: var str = '內容';
str = str.replace('被替換的內容','替換以后的內容'); (替換以后的內容可以是字,也可以是標簽)
document.write(str);
ajax核心:XMLHttpRequest(需要在服務器環境下)
ajax原理:
- 創建ajax對象:
不兼容IE6:
var oAjax = new XMLHttpRequest();
兼容IE678:
var oAjax = new ActiveXObject('Microsoft.XMLHTTP');
兼容寫法:
if(window.XMLHttpRequest){
var oAjax = new XMLHttpRequest();
}else{
var oAjax = new ActiveXObject('Microsoft.XMLHTTP');
- 打開鏈接:
oAjax.open('打開方式','url?data',是否異步);
同步:一次只能做一件事
異步:同時做多件事
- 發送請求:
oAjax.send();
- 接收響應:
oAjax.onreadystatechange = function(){
判斷ajax狀態碼
if(oAjax.readyState==4){
判斷http狀態碼
if(oAjax.status>=200&&oAjax.status<300||oAjax.status==304){
成功
oAjax.responseText
響應文本
}else{
失敗
}
}
}
get請求:
if(window.XMLHttpRequest){
var oAjax = new XMLHttpRequest();
}else{
var oAjax = new ActiveXObject('Microsoft.XMLHTTP');
oAjax.open('GET','xxx?xxx=xxx',true);
oAjax.send();
oAjax.onreadystatechange = function(){
判斷ajax狀態碼
if(oAjax.readyState==4){
判斷http狀態碼
if(oAjax.status>=200&&oAjax.status<300||oAjax.status==304){
成功
oAjax.responseText
響應文本
}else{
失敗
}
}
}
post請求:
if(window.XMLHttpRequest){
var oAjax = new XMLHttpRequest();
}else{
var oAjax = new ActiveXObject('Microsoft.XMLHTTP');
oAjax.open('POST','url',true);
設置請求頭部
oAjax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
oAjax.send(data);
oAjax.onreadystatechange = function(){
判斷ajax狀態碼
if(oAjax.readyState==4){
判斷http狀態碼
if(oAjax.status>=200&&oAjax.status<300||oAjax.status==304){
成功
oAjax.responseText
響應文本
}else{
失敗
}
}
}
ajax狀態碼:
0 準備成功,未發送
1 發送成功
2 接收原始數據成功
3 解析數據成功
4 完成
HTTP狀態碼 (3位數)
2字頭代表成功
304 重定向(Not Modify)
跨域的方法:
jsonp **(基本上用jsonp)**
設置代理
iframe
window.open('地址','打開方式');
打開方式: _self
_blank
jsonp
例如:
百度下拉:(https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=a&cb=show)
其中: wd→→word 關鍵詞
cb→→callback 回調函數
ajax封裝:
function json2url(json){
var arr = [];
for(var key in json){
arr.push(key+'='+json[key]);
}
return arr.join('&');
}
function ajax(options){
options = options||{};
if(!options.url)return;
options.data = options.data||{};
options.data.t = Math.random();
options.type = options.type||'get';
var data = json2url(options.data);
if(window.XMLHttpRequest){
var oAjax = new XMLHttpRequest();
}else{
var oAjax = new ActiveXObject('Microsoft.XMLHTTP');
}
switch(options.type.toLowerCase()){
case 'get':
oAjax.open('GET',options.url+'?'+data,true);
oAjax.send();
break;
case 'post':
oAjax.open('POST',options.url,true);
oAjax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
oAjax.send(data);
break;
default:
oAjax.open('GET',options.url+'?'+data,true);
oAjax.send();
break;
}
oAjax.onreadystatechange = function(){
if(oAjax.readyState==4){
if(oAjax.status>=200&&oAjax.status<300||oAjax.status==304){
options.success&&options.success(oAjax.responseText);
}else{
options.error&&options.error(oAjax.status);
}
}
};
}
ajax({
url:'',
data:{},
type:'',
success:function(res){
alert(res);
},
error:function(err){
alert('失敗:'+err)
}
});