AJAX

AJAX(Asynchronous JavaScript and XML) 异步的javascript和XML

  1. 普通方式创建XHR对象发送ajax请求

     var xhr = new XMLHttpRequest()
    

1.1 open(method,url,async)

   /**
     * method 指定请求的方式 一般有GET/POST
     * url 指定请求的地址 可以是绝对地址也可以是相对地址
     * async 指定是否是异步请求 一般为true 即发送一个异步请求
     */

1.2 send(string)

 /**
   * string 代表请求的参数
   */

1.3 举例说明

//GET请求
var xhr = new XMLHttpRequest();
xhr.open(GET,"UserController.do",true);
xhr.send();
//POST请求
var xhr = new XMLHttpRequest();
xhr.open(POST,"UserController.do",true);
xhr.send(user);
  1. 使用jQuery发送ajax请求(推荐)

     $.ajax({
       type: "GET",//请求类型
       url: "/user/UserController.do" //请求地址
       data:{
           userId: "123"
       }, //请求参数
       dataType: "json", //返回数据格式
       success: function(res){ //成功回调函数
           console.log(res)
       },
       error: function(err){ //失败回调函数
           alert(err.status)
       }
     });
    
  2. ajax跨域问题
    只要协议、域名、子域名、端口有任何一个不同,都被当作是不同的域。

解决跨域https://www.cnblogs.com/lxwphp/p/8080188.html

你可能感兴趣的:(AJAX)