跨域发送异步请求的两种方式
作者:互联网
跨域发送异步请求的两种方式
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="http://localhost:8060/JSON_war_exploded/jquery.min.js"></script>
<script>
function funmove(){
//id的属性用$("#id")获取
//判断她为空的话,给span一个文本发生
if (null== $("#userid").val()||''==$("#userid").val()){
$("#spanid").text("用户名不能为空")
return
}
//这里是用户看到后进行输入,再次执行清空sqan的操作;就不执行if了
$("#spanid").text("")
//发送异步请求
$.ajax(
{
type:"GET",//method的方式
url:"http://localhost:8060/JSON_war_exploded/unameCheckServlet.do?",// 请求的后台服务的路径
data:{username:$("#userid").val()},// 提交的参数
dataType:"jsonp",
jsonp:"aaa",
success:function (msg) {// 响应成功执行的函数
$("#spanid").text(msg)
}
}
)
}
</script>
</head>
<body>
<form method="get" action="unameCheckServlet.do">
账号:<input id="userid" name="username" type="text" onblur="funmove()">
<span id="spanid" style="color: red"></span><br/>
密码:<input name="password" type="password"><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="http://localhost:8060/JSON_war_exploded/jquery.min.js"></script>
<script>
function funmove(){
//id的属性用$("#id")获取
//判断她为空的话,给span一个文本发生
if (null== $("#userid").val()||''==$("#userid").val()){
$("#spanid").text("用户名不能为空")
return
}
//这里是用户看到后进行输入,再次执行清空sqan的操作;就不执行if了
$("#spanid").text("")
//发送异步请求
$.getJSON(
"http://localhost:8060/JSON_war_exploded/unameCheckServlet.do?jsoncallback=?",
{username:$("#userid").val()},
function (msg) {// 响应成功执行的函数
$("#spanid").text(msg)
}
)
}
</script>
</head>
<body>
<form method="get" action="unameCheckServlet.do">
账号:<input id="userid" name="username" type="text" onblur="funmove()">
<span id="spanid" style="color: red"></span><br/>
密码:<input name="password" type="password"><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
标签:异步,跨域,val,text,userid,发送,msg,spanid,id 来源: https://www.cnblogs.com/188221creat/p/16335321.html