发表于: 2020-03-02 21:34:43
1 1684
今日完成
jQuery ajax - serialize() 方法
定义和用法
serialize() 方法通过序列化表单值,创建 URL 编码文本字符串。
可以选择一个或多个表单元素(比如 input 及/或 文本框),或者 form 元素本身。
序列化的值可在生成 AJAX 请求时用于 URL 查询字符串中。
语法
$(selector).serialize()
JavaScript Ajax
var aValue = document.getElementsByTagName('input');
var oMsg = document.getElementById('msg');
var oBtn = document.getElementById('btn');
var timer = null;
oBtn.onclick = function(event) {
event.preventDefault();
var name = aValue[0].value;
var pwd = aValue[1].value;
var data = "name=" + name + "&pwd=" + pwd;
var oAjax = new XMLHttpRequest();
oAjax.onreadystatechange = function() {
if (oAjax.readyState == 4 && oAjax.status == 200) {
var resdata = JSON.parse(oAjax.responseText);
console.log(name);
console.log(pwd);
console.log(resdata);
if (resdata.code === 0) {
window.location.href = "http://dev.admin.carrots.ptteng.com/";
} else {
clearInterval(timer);
oMsg.innerHTML = resdata.message;
timer = setTimeout(function() {
oMsg.innerHTML = '';
}, 3000)
}
}
}
oAjax.open('POST', '/carrots-admin-ajax/a/login', true);
oAjax.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
oAjax.send(data);
}
评论