Ajax

请求方式: get/psot  

    get: 不发送数据,只通过url传入一点数据(有长度限制,可以获取本地文件)

    post:发送大量数据(没有长度限制,不能获取本地文件)

同源策略: 

    同源策略(Sameoriginpolicy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响。可以说Web是构建在同源策略基础之上的,浏览器只是针对同源策略的一种实现。同源策略会阻止一个域的javascript脚本和另外一个域的内容进行交互。所谓同源(即指在同一个域)就是两个页面具有相同的协议(protocol),主机(host)和端口号(port)。也就是说当前页面只要是协议、主机、端口有一项不同,就会一律不准访问。

固定写法:

  

 1         function ajax(data,type="GET"){
 2             if(type!=="GET") type="POST";
 3             return new Promise(function(resolve,reject){
 4                 let getData="";
 5                 let postData="";
 6                 if(type==="GET"){
 7                     getData="?"+QueryString.stringfiy(data);
 8                 }else{
 9                     postData=QueryString.stringfiy(data);
10                 }
11                 let xhr=new XMLHttpRequest();
12                 xhr.open(type,"http://127.0.0.1:8001"+getData);
13                 xhr.onreadystatechange=function(){
14                     if(xhr.readyState<4) return;
15                     if(xhr.status===200){
16                         resolve(xhr.response);
17                     }else{
18                         reject(xhr.response);
19                     }
20                 }
21                 xhr.onerror=function(){
22                     reject("地址错误");
23                 }
24                 xhr.send(postData);
25             })
26         }

  参数: 

     xhr.open("GET","http://127.0.0.1:8001",true,user,password);     (请求方式,请求地址,是否异步默认true,访问通信时的用户名、密码)

原文地址:https://www.cnblogs.com/wangjingzhi/p/12334324.html