javascript中是用fetch和axios发送请求用法
2019/1/29 10:54:40 | 阅2690 | 来源:好空间网络 [打印] [关闭] |
fetch('https://xxx.com/?appid=123456&username='+username+'&password='+password+'&date='+new Date().getTime()) .then(data => { if (data.ok) { return data.json() }else{ return Promise.reject('请求失败') } }) .then(data => { console.log(data.isstatus) if(data.isstatus==true){ localStorage.setItem("username", data.username, 24 * 60 * 60,); localStorage.setItem("openid", data.openid, 24 * 60 * 60,); MIP.viewer.open('shop.html', {replace: true}) }else{ document.getElementById("tishi").innerHTML="登陆失败:用户名或密码错!"; } }) .catch(error => console.log('请求失败:', error)); }
使用用axios请求,
axios .get('https://xxxx.com/?appid=123456&md5=31D1D597E5E26E6B4CACC74FF54E0193&openid='+localStorage.openid) .then(response => (this.list = response.data))
看起来axios比较简单3行代码而已,但是这个需要先引入
<script src="https://cdn.bootcss.com/axios/0.18.0/axios.js"></script>
axios的js文件
试用fetch以post方式提交
fetch('xxx.com', { method: 'post', headers: { "Content-type": "application/x-www-form-urlencoded; charset=UTF-8" }, body: 'username=bar&password=ipsum' }) .then(data => { if (data.ok) { return data.json() }else{ return Promise.reject('请求失败') } }) .then(data => { console.log('Request succeeded with JSON response', data); }) .catch(error => { console.log('Request failed', error); });