温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ajax中怎么通过异步加载实现局部刷新

发布时间:2021-08-07 17:11:15 来源:亿速云 阅读:95 作者:Leah 栏目:web开发

这篇文章给大家介绍ajax中怎么通过异步加载实现局部刷新,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

[html] view plain copy

  1. <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-2.1.0.js"></script>  

  2. <script type="text/javascript">  

  3.     $(function(){  

  4.         $("#inp1").blur(function(){  

  5.             var name=$(this).val();  

  6.             $.ajax({  

  7.                 url:"${pageContext.request.contextPath}/demo",  

  8.                 data:{"uname":name,"upwd":"jack123","age":12},  

  9.                 cache:"false",  

  10.                 async:"true",  

  11.                 dataType:"json",  

  12.                 type:"post",  

  13.                 success:function(data){  

  14.                   

  15.                     if(data.flag=="1"){  

  16.                         $("#sp").html("用户名存在");  

  17.                     }else{  

  18.                         $("#sp").html("");  

  19.                     }  

  20.                 },  

  21.                 error:function(){  

  22.                     alert("服务器端异常");  

  23.                 }  

  24.                   

  25.                   

  26.                   

  27.             });  

  28.               

  29.               

  30.               

  31.         });  

  32.       

  33.           

  34.           

  35.           

  36.     })  

  37.   

  38.   

  39.   

  40. </script>  

  41.   

  42.   

  43. <title>Insert title here</title>  

  44. </head>  

  45. <body>  

  46. <input type="text" value="" id="inp1"><span id="sp"></span>  

  47. </body>  

  48. </html>  

jquery封装的ajax 进行数据交互,  实现异步加载

<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-2.1.0.js"></script>


<script type="text/javascript">
var json={
"flag":"1" 
};
//alert(json.flag);
$(function(){
//$.get(url,data,success(data,status,xhr),dataType)
$("#btn").click(function(){
$.get(
"${pageContext.request.contextPath}/demo",
{"uname":"jack","upwd":"123","age":12},
function(data,status){
//var data={"flag":"1"}
alert(data);

if(data.flag=="1"){
$("#sp").html("用户名已经被占用").css("color","red");
}else{
$("#sp").html("用户名可以使用").css("color","green");
}

},
"json"
);
})
});
</script>
</head>
<body>
<input type="button" id="btn" value="$.get异步请求服务器"><span id="sp">
</body>

关于ajax中怎么通过异步加载实现局部刷新就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI