注意:网页文件和php文件处于同一站点下面,网页文件以地址方式打开。不然会出现跨域访问的问题
window.onload=function () {
var ajax=document.getElementsByClassName('ajax');
ajax[2].onclick=function(){
var name=ajax[0].value;
console.info(name);
//jquery ajax
$.ajax({
url:'http://localhost/ajax/ajax.php',
data:{
name:name,
pwd:123,
},
type:'GET', //post 改这里就行
success:function(res)
{
console.info(res);
},
})
//js Ajax GET
// var ajx=new XMLHttpRequest();
// ajx.onreadystatechange=function()
// {
// if(ajx.readyState===4&&ajx.status===200)
// {
// console.info(ajx.responseText);
// }
// }
// ajx.open('GET','http://localhost/ajax/ajax.php?name='+name);
// ajx.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// ajx.send();
//js Ajax POST
// var ajx=new XMLHttpRequest();
// ajx.onreadystatechange=function()
// {
// if(ajx.readyState===4&&ajx.status===200)
// {
// console.info(ajx.responseText);
// }
// }
// ajx.open('POST','http://localhost/ajax/ajax.php');
// ajx.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// ajx.send('name='+name);
}
}
ajax 原生和jquery的简单用法
最新推荐文章于 2021-10-28 22:47:46 发布