加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

原生ajax写法

发布时间:2020-12-16 02:56:47 所属栏目:百科 来源:网络整理
导读:转自:https://blog.csdn.net/qq_empire/article/details/81737394 相关链接:https://blog.csdn.net/qq_30101879/article/details/77916622 原生ajax使用: ? function ajax(url){?? ??? ?var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : Active

转自:https://blog.csdn.net/qq_empire/article/details/81737394

相关链接:https://blog.csdn.net/qq_30101879/article/details/77916622

原生ajax使用:

?  function ajax(url){
?? ??? ?var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : ActiveXObject("microsoft.XMLHttp")
?? ??? ?xhr.open("get",url,true);
?? ??? ?xhr.send();
?? ??? ?xhr.onreadysattechange = () =>{
?? ??? ??? ?if(xhr.readystate == 4){
?? ??? ??? ??? ?if(xhr.status == 200){
?? ??? ??? ??? ??? ?var data = xhr.responseTEXT;
?? ??? ??? ??? ??? ?return data;
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}?? ?
?? ?}
 

get方式

    function btnClick() {
        //创建核心对象
        xmlhttp = null;
        if (window.XMLHttpRequest) {// code for Firefox,Opera,IE7,etc.
            xmlhttp = new XMLHttpRequest();
        } else if (window.ActiveXObject) {// code for IE6,IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        //编写回调函数
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                alert(xmlhttp.responseText)
            }
        }
        //open设置请求方式和请求路径
        xmlhttp.open("get","/Ajax/ajax2?username=张三");//一个servlet,后面还可以写是否同步
        //send 发送
        xmlhttp.send();
    }

post

    function btnClick() {
        //创建核心对象
        xmlhttp = null;
        if (window.XMLHttpRequest) {// code for Firefox,IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        //编写回调函数
        xmlhttp.onreadystatechange = function() {
            /*     alert(xmlhttp.readyState); */
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                alert(xmlhttp.responseText)
            }
            /* alert(123); */
        }
        //open设置请求方式和请求路径
        xmlhttp.open("post","/Ajax/ajax2");//一个servlet,后面还可以写是否同步
        //设置请求头
        xmlhttp.setRequestHeader("content-type","application/x-www-form-urlencoded")
        //send 发送
        xmlhttp.send("username=张三");
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读