小小程序猿
这个人很懒,什么都没写
Toggle navigation
小小程序猿
主页
关于
友链
归档
标签
js xhr
js
2022-04-21 19:44:03
40
0
0
terry
js
```javascript // 一个简单的http post 最原始的ajax 请求方法 function xhrLoadText(url, onSuccess, onFailure, data, isCookie) { onSuccess = onSuccess || function _onSuccess(data) {}; onFailure = onFailure || function _onFailure() {}; var xhr = new XMLHttpRequest(); xhr.withCredentials = isCookie ? true : false; //如果传递ck 到后台,需要设置true xhr.open('POST', url, true); xhr.onreadystatechange = function() { if (xhr.readyState == 4) { if (xhr.status == 200 || xhr.status === 0) { onSuccess(xhr.responseText); console.log( xhr.getResponseHeader('ck') ); console.log( xhr.getResponseHeader('Content-Type') ); } else { onFailure(); } } } xhr.onerror = onFailure; xhr.ontimeout = onFailure; try { let x = new FormData(); if (data && Object.keys(data).length > 0) { let k = Object.keys(data); for (let i = 0; i < k.length; i++) { x.append(k[i], data[k[i]]); } } console.log(x); xhr.send(x); } catch (e) { onFailure(); } } xhrLoadText('http://localhost/user/validlogin.do',(s)=>{console.log(s)},(e)=>{console.log(e)},{'userName':'test001','userPassword':'666666'},false) ```
上一篇:
回忆指针
下一篇:
linux-arp
0
赞
40 人读过
新浪微博
微信
腾讯微博
QQ空间
人人网
提交评论
立即登录
, 发表评论.
没有帐号?
立即注册
0
条评论
More...
文档导航
没有帐号? 立即注册