ajax基础

ajax运行原理

 

 

 在服务器和浏览器之间操作

实现步骤:

1.创建ajax对象

var xmlhttp;
if (window.XMLHttpRequest)
{
    //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
    xmlhttp=new XMLHttpRequest();
}
else
{
    // IE6, IE5 浏览器执行代码
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

2.告诉ajax请地址以及请求方式

xmlhttp.open("GET","ajax_info.txt",true);

3.发送请求

xmlhttp.send()

4.获取服务器端给客户端的数据响应

xmlhttp.onload = function(){
  console.log(xhr.responseText)
}

 

ajax简洁版写法,该方法更加灵活,有很多参数可以自己设置,注意传递json或者form参数的时候要修改一些设置

  $.ajax({
                type: "get",
                url: "http://localhost:4000/download_test",
                beforeSend: function () {
                    addDiv()
                },
                success: function (msg) {
                    if (msg == "erro") {
                        alert("没有选择测试图,重新选择");
                        return
                    }
                    deleteDiv()
                    console.log("上传完成")
                    file_name = msg
                    download2browser(file_name)
                },
                error: function (e, jqxhr, settings, exception) {
                    deleteDiv()
                    alert("下载失败!");
                }
            })

 

posted @ 2022-03-17 22:35  开锁球  阅读(27)  评论(0)    收藏  举报