JS的Ajax对象

  //1.得到对象
    var ajax = new XMLHttpRequest();

    //2.建立连接
    ajax.open('post','./2.php',true);

    /*
        使用post发送数据
        需要设置一个请求头
        描述数据类型
     */
    ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded");

    //发送
    ajax.send('id=5&name=zhangsan&age=18');
    //准备事件
    ajax.onreadystatechange = function(){
        //判断
        if (ajax.readyState == 4 && ajax.status == 200) {
            //处理带回的数据
            var result = ajax.responseText;
            alert(result);
        }
    }

猜你喜欢

转载自www.cnblogs.com/dbhui/p/9194143.html