关于javascript:全方位了解JavaScript实现项目对接短信验证码FetchjQueryXHR太实用了

42次阅读

共计 1670 个字符,预计需要花费 5 分钟才能阅读完成。

短信验证码注册登录就是验证用户提交的手机验证码,如果验证码验证谬误,就提醒“验证码谬误”,如果验证正确,那就进入到注册胜利的页面。

这几天始终在钻研 JavaScript 实现短信验证码注册登录的办法,次要是因为我的项目需要(当初如同简直所有的我的项目都须要验证码注册登录),整顿了三种对接办法,可供大家学习和参考:

Fetch 办法
`var myHeaders = new Headers();
myHeaders.append(“Content-Type”, “application/x-www-form-urlencoded;charset=utf-8”);

var urlencoded = new URLSearchParams();
urlencoded.append(“appId”, “41KYR0EB**”);
urlencoded.append(“appKey”, “IIWCKKSR7NOQ**”);
urlencoded.append(“phone”, “1561894**”);
urlencoded.append(“templateId”, “1043”);
urlencoded.append(“variables”, “1234”);

var requestOptions = {
method: ‘POST’,
headers: myHeaders,
body: urlencoded,
redirect: ‘follow’
};

fetch(“https://vip.veesing.com/smsApi/verifyCode”, requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log(‘error’, error));`
jQuery 办法
`var settings = {
“url”: “https://vip.veesing.com/smsApi/verifyCode”,
“method”: “POST”,
“timeout”: 0,
“headers”: {

"Content-Type": "application/x-www-form-urlencoded;charset=utf-8"

},
“data”: {

"appId": "41KYR0EB****",
"appKey": "IIWCKKSR7NOQ****",
"phone": "1561894****",
"templateId": "1043",
"variables": "1234"

}
};

$.ajax(settings).done(function (response) {
console.log(response);
});`
XHR 办法
`var data = “appId=41KYR0EB&appKey=IIWCKKSR7NOQ&phone=1561894**&templateId=1043&variables=1234″;

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener(“readystatechange”, function() {
if(this.readyState === 4) {

console.log(this.responseText);

}
});

xhr.open(“POST”, “https://vip.veesing.com/smsApi/verifyCode”);
xhr.setRequestHeader(“Content-Type”, “application/x-www-form-urlencoded;charset=utf-8”);

xhr.send(data);`
以上就是 JavaScript 实现短信验证码登录注册的 3 种办法,有疑难能够在评论区指出,欢送和大家一起探讨。
JavaScript – Fetch.js、JavaScript – jQuery、JavaScript – XHR.js 文件下载

正文完
 0