js:
//显示用户信息
$.ajax("http://ltest.com:2020/test/",{
//提交数据的类型 POST GET
type:"POST",
async:false,
data:{ 'username': username.value, 'password': userpassw.value },
crossDomain:true,
datatype: "json",//"xml", "html", "script", "json", "jsonp", "text".
xhrFields: { withCredentials: true },
//成功返回之后调用的函数
success:function(data,status,xhr){
if(data["state"]==1)
{
alert("ok");
}else{
alert("密码或用户名错误");
}
}
});
服务端要加头:
Access-Control-Allow-Origin 不能为“*“,当withCredentials为真是
resp['Access-Control-Allow-Origin'] = req.META.get("HTTP_ORIGIN")
“true”不要写成“True”了,浏览器不认
resp["Access-Control-Allow-Credentials"] = "true"