原生XMLHttp发送
function login() {
var username =document.getElementById("username").value;
var password = document.getElementById("password").value;
var remember =document.getElementById("remember").checked;
var xmlhttp;
if (window.XMLHttpRequest)
{
// IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
xmlhttp=new XMLHttpRequest();
}
else
{
// IE6, IE5 浏览器执行代码
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState===4 && xmlhttp.status===200)
{
...
}
};
xmlhttp.open("POST","/flask/login",true);
xmlhttp.setRequestHeader("Content-type","application/json");
// 后面这两部很重要,我看网上很多都是使用xmlhttp.send("username="+username+"&password="+"),这样接收还要解析一番感觉还是直接发送以下格式的好些
var data = {
"username": username
"password": password
"remember": remember
};
var data_json = JSON.stringify(data);
xmlhttp.send(data_json);
}
json数据解析
var text = xmlhttp.responseText;
// 通过eval() 方法将json格式的字符串转化为js对象,并进行解析获取内容
var result = eval("("+text+")");
if (result) {
} else {
alert("请输入正确的用户名和密码");
}
ajax发送
$(document).ready(function () {
var data = {
"username": "adamin",
"password": "123456789",
"remember": true
}
$.ajax({
url: "/flask/login",
type: "POST",
data: data,
success: function () {
}
})
})