AJAX

2017-06-05  本文已影响0人  osoLife

AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术。

一个简单的GET请求

// HTML
<h1>hello world</h1>
<button id="btn">修改内容</button>

// JS
var oH1=document.getElementsByTagName('h1')[0];
var oBtn=document.getElementById('btn');
oBtn.onclick=function(){
    var xhr=new XMLHttpRequest();
    xhr.onreadystatechange=function(){
        if(xhr.readyState==4 && xhr.status==200){
            oH1.innerHTML=xhr.responseText;
        }
    };
    xhr.open('GET','test.php',true);
    xhr.send();
};

// PHP
echo "hello ajax";
$.ajax({
    url:'test.php',
    type:'GET',
    dataType:'json',
    data:{

    },
    success:function(data){

    },
    error:function(){
        alert('error');
    },
    complete:function(){
                
    }
});

有参数的GET

// PHP
echo "你的名字是:".$_GET['name']."你的年龄是:".$_GET['age'];

POST

xhr.open('POST','test.php',true);
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xhr.send('name=osoLife&age=100');
上一篇 下一篇

猜你喜欢

热点阅读