js 发送异步请求
js用XMLHttpRequest发送异步请求
发送GET请求
var xhr = new XMLHttpRequest();
xhr.open('GET',url);//url为请求地址
xhr.responseType = 'json';
xhr.onload = function () {
   // 请求结束后,在此处写处理代码
};
xhr.onerror = function(){//请求错误
    console.log('xhr error');
}
xhr.send();
发送POST请求
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
//发送合适的请求头信息
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {//Call a function when the state changes.
    if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) {
        // 请求结束后,在此处写处理代码
    }
}
xhr.send("foo=bar&lorem=ipsum"); 
Fetch发送请求 除了IE和Safari浏览器不支持,别的浏览器大多提供了支持。(现在Safari也即将为fetch和promise提供支持)
//fetch()返回一个promise,它将解析从服务器发回的响应。我们使用then()来运行一些后续代码
fetch(url).then(function(response){
    //处理text/html响应 相当于  xhr.responseType = 'json';
    //return response.text();
    //json响应
    return response.json();
}).then(function(data){ //相当于xhr.onload =function(){}
    console.log(data);
}).catch(function(e){//相当于xhr.onerror =function(){}
    console.log('error' + e);
});
获取头信息:
fetch(url).then((response)=>{
    console.log(response.status);
    console.log(response.statusText);
    console.log(response.headers.get('Content-Type'));
    console.log(response.headers.get('Date'));
    return response.json();
}).then(function(data){
    console.log(data);
})
  .catch(function(e){
    console.log('error' + e);
});
设置头信息
fetch(url,{
    headers:{
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    }
}).then((response)=>{
    return response.json();
}).then(function(data){
    console.log(data);
})
  .catch(function(e){
    console.log('error' + e);
});
提交表单
fetch(url,{
    method: 'post',
    body: new FormData(document.getElementById('form'))
}).then((response)=>{
    return response.json();
}).then(function(data){
    console.log(data);
})
  .catch(function(e){
    console.log('error' + e);
});
提交json数据
fetch(url,{
    method: 'post',
    body: JSON.stringify({
        username: document.getElementById('username').value,
        password: document.getElementById('password').value
    })
}).then(function(data){
    console.log(data);
})
  .catch(function(e){
    console.log('error' + e);
});
Fetch浏览器兼容


本文摘抄于
XMLHttpRequest
js 发送异步请求的更多相关文章
- AJAX的来龙去脉(由来)-如何被封装出来的--ajax发送异步请求(四步操作)
		<黑马程序员_超全面的JavaWeb视频教程vedio\JavaWeb视频教程_day23_ajax> \JavaWeb视频教程_day23_ajax\day23ajax_avi\14.打 ... 
- 使用AJAX技术发送异步请求,HTTP服务端推送
		使用AJAX技术发送异步请求 什么是AJAX AJAX指一步Javascript和XML(Asynchronous JavaScript And XML),它是一些列技术的组合,简单来说AJAX基于X ... 
- 16 react  发送异步请求获取数据 和 使用Redux-thunk中间件进行 ajax 请求发送
		1.发送异步请求获取数据 1.引入 axios ( 使用 yarn add axios 进行安装 ) import axios from 'axios'; 2. 模拟 在元素完成挂载后加载数据 并初始 ... 
- AJAX发送异步请求教程详解
		AJAX 一.AJAX简介 什么是 AJAX ? AJAX = 异步 JavaScript 和 XML. AJAX 是一种用于创建快速动态网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可 ... 
- 原生js发送ajax请求
		堕落了一阵子了,今天打开博客,发现连登录的用户名和密码都不记得了.2016年已过半,不能再这么晃荡下去了. 参加了网易微专业-前端攻城狮 培训,目前进行到大作业开发阶段,感觉举步维艰.但是无论如何,不 ... 
- node.js 发送http 请求
		自己研究了一下 node.js 的 http模块 下面为想服务器发送请求的代码 ,通过学习了解http 请求的过程 ,node.js 对http请求的原始封装比较低,以前php 可以用$_GET , ... 
- .Net core webapi使用httpClient发送异步请求遇到TaskCanceledException: A task was canceled
		前言:本人最近较多使用.net core的项目,最近在使用httpClient发送请求的时候,遇到服务器处理时间较长时,就老是会报异常:TaskCanceledException: A task wa ... 
- JQ+AJAX 发送异步请求
		1. load() ; 作用:通过ajax 请求从服务器加载数据,并添加到符合要求的节点上 用法:$node.load(请求地址,请求参数) 请求参数写法: --"username=admi ... 
- js fetch异步请求使用详解
		目录 认识异步 fetch(url) response.json() 结合async和await 异常处理 post请求 认识异步 首先我们得明白请求是一个异步的过程. 因为请求需要时间向服务器发送请 ... 
随机推荐
- 【闭包】JS闭包深入理解
			先看题目代码: 1 2 3 4 5 6 7 8 9 10 11 12 function fun(n,o) { console.log(o) return { fun:function(m){ ... 
- LC 358. Rearrange String k Distance Apart
			Given a non-empty string s and an integer k, rearrange the string such that the same characters are ... 
- docker commit命令
			docker commit命令用于基于一个容器来创建一个新的docker镜像. docker commit制作的镜像,除了制定镜像的人知道执行过什么命令,怎么生成的镜像,别人根本无从得知.建议使用的是 ... 
- php文件上传系统
			一般10M以下的文件上传通过设置Web.Config,再用VS自带的FileUpload控件就可以了,但是如果要上传100M甚至1G的文件就不能这样上传了.我这里分享一下我自己开发的一套大文件上传控件 ... 
- Python浮点型数据小数点的取舍
			python默认的是17位小数的精度 1.round()内置方法 π=3.1415926535 new_num=round(π,2) #四舍五入保留两位小数 print(new_num) ... 
- Python爬虫学习==>第十二章:使用 Selenium 模拟浏览器抓取淘宝商品美食信息
			学习目的: selenium目前版本已经到了3代目,你想加薪,就跟面试官扯这个,你赢了,工资就到位了,加上一个脚本的应用,结局你懂的 正式步骤 需求背景:抓取淘宝美食 Step1:流程分析 搜索关键字 ... 
- FTP简单搭建(二)
			六.配套设置 1.基于用户名的上传和下载 创建用户 useradd alex echo redhat |passwd --stdin alex 指定用户登录的路径 可不设置,不设置则为用户家目录 mk ... 
- ubuntu 16.04 关闭开启图形界面
			说明案例:ubuntu16.04 关闭图形界面命令: systemctl disable lightdm.service 开启图形界面命令: ln -s /lib/systemd/system/lig ... 
- Windows Server 中配置权威时间服务器
			0" style="box-sizing: inherit; outline: none;"> 若要配置 Windows 时间服务以使用内部硬件时钟,请使用下列方法 ... 
- C学习笔记-文件操作
			文件操作大致分三步 打开文件 读写文件 关闭文件 二进制和文本模式的区别 在windows系统中,文本模式下,文件以"\r\n"代表换行.若以文本模式打开文件,并用fputs等函数 ... 
