Ajax使用

Javascript Ajax

  1. myurl="/Shop/GoodsSer?status=sendToCart&gid=1";
  2. getResult(myurl);
  3. var xmlhttp;
  4. function getResult(url){
  5. //创建XMLHttpRequest对象
  6. if(window.XMLHttpRequest){
  7. xmlhttp=newXMLHttpRequest();
  8. }
  9. else{
  10. xmlhttp=newActiveXObject("Microsoft.XMLHTTP");
  11. }
  12. if(xmlhttp){
  13. //打开url请求
  14. xmlhttp.open("GET",url,true);
  15. xmlhttp.setRequestHeader("Content-Type","text/html;charset=UTF-8");
  16. //请求被发送到服务器时,执行响应任务
  17. xmlhttp.onreadystatechange = complete;
  18. //发送
  19. xmlhttp.send(null);
  20. }
  21. }
  22. function complete(){
  23. //表示请求过程 0->4
  24. if(xmlhttp.readyState==4){
  25. //请求结果状态
  26. if(xmlhttp.status==200){
  27. var info=xmlhttp.responseText;
  28. }
  29. }
  30. }
 
Jquery Ajax
  1. $.ajax({
  2. type :"post/get",
  3. async :false/true,
  4. url :"*.do",
  5. data :{data1:$("").val(),data2:$("").val()},
  6. dataType :"text/xml/json",
  7. success :function(result){
  8. handler...
  9. }
  10. });
  11. $.post("login.do",{
  12. data1 : $('').val(),
  13. data2 : $('').val()
  14. },function(data){
  15. handler...
  16. },"json").error(function(){...};
  17. $.post("login.do", $("#form").serialize(),function(data){
  18. handler...
  19. },"json").error(function(){...};
 
动态加载js
  1. jQuery.getScript("static/library/plugins/city-select/jquery.cityselect.js",
  2. function(data, status, jqxhr){
  3. $("#city").citySelect({
  4. prov:"1", city:"1", dist:"1"
  5. });
  6. })
  7. });
 
/******************************************************************/
 
例子
  1. <script type="text/javascript">
  2. function send(){
  3. $("#login").animate({opacity:0.5},1000,function(){
  4. var uname = $("#uname").val();
  5. var pwd = $("#pwd").val();
  6. var _url ="/Chat/Login";
  7. $.ajax({url:_url, type:"get", dataType:"html", data:{action:'login',uname:uname,pwd:pwd},
  8. beforeSend:function(){
  9. $('#resDv').text("正在登录......");
  10. $('#resDv').fadeIn("slow");
  11. },
  12. error:function(){alert("登录失败!")}
  13. ,success:function(msg){
  14. $('#resDv').fadeOut("slow",function(){
  15. $("#login").animate({opacity:100},2000);
  16. $("#uname").val("");
  17. $("#pwd").val("");
  18. location.href="main/index.jsp";
  19. });
  20. }
  21. });
  22. })
  23. }
  24. </script>
 
 
 





原文地址:https://www.cnblogs.com/mmyh/p/6022690.html