<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    js文件(yizhi_ajax.js)
      1      /***  僅作為學習交流之用
      2         *  請您勿用于商業用途
      3         */
     
      4      var yizhi_ajax ={
      5          xhr:false,
      6          url:"",
      7          method:"post",
      8          toString:Object.prototype.toString,
      9          isArray:function( obj ) {
     10            return this.toString.call(obj) === "[object Array]";
     11        }
    ,
     12        isFunction:function( obj ) {
     13            return this.toString.call(obj) === "[object Function]";
     14        }
    ,
     15        each:function( object, callback, args ) {
     16            var name, i = 0, length = object.length;
     17            if ( args ) {
     18                if ( length === undefined ) {
     19                    for ( name in object )
     20                        if ( callback.apply( object[ name ], args ) === false )
     21                            break;
     22                }
     else
     23                    for ( ; i < length; )
     24                        if ( callback.apply( object[ i++ ], args ) === false )
     25                            break;
     26            }
     else {
     27                if ( length === undefined ) {
     28                    for ( name in object )
     29                        if ( callback.call( object[ name ], name, object[ name ] ) === false )
     30                            break;
     31                }
     else
     32                    for ( var value = object[0];
     33                        i < length && callback.call( value, i, value ) !== false; value = object[++i] ){}
     34            }

     35    
     36            return object;
     37        }
    ,
     38          param:function(a){
     39              var s = [ ];
     40            function add( key, value ){
     41                s[ s.length ] = encodeURIComponent(key) + '=+ encodeURIComponent(value);
     42            }
    ;
     43            if ( this.isArray(a))
     44                this.each( a, function(){
     45                    add( this.name, this.value );
     46                }
    );
     47    
     48            else
     49                for ( var j in a )
     50                    if ( this.isArray(a[j]) )
     51                        this.each( a[j], function(){
     52                            add( j, this );
     53                        }
    );
     54                    else
     55                        add( j, this.isFunction(a[j]) ? a[j]() : a[j] );
     56            return s.join("&").replace(/%20/g, "+");
     57          }
    ,
     58          showMsg:true,
     59          getXhr:function(){
     60                  if(this.xhr==false){
     61                  try{
     62                      this.xhr = new ActiveXObject("Microsoft.XMLHTTP");
     63                  }
    catch(e){
     64                      try{
     65                          this.xhr = new ActiveXObject("Msxml2.XMLHTTP");
     66                      }
    catch(e){
     67                          alert("xhr對象創建失敗!");
     68                      }

     69                  }

     70              }

     71              return this.xhr;
     72          }
    ,
     73          send:function(requestUrl,parameters,callBack){
     74              if(this.xhr==false){
     75                  this.getXhr();
     76              requestUrl+=("?"+new Date().getTime()); //利用隨即生成的時間解決緩存問題
     77              this.xhr.open(this.method,requestUrl,true);
     78              this.xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded;charset=UTF-8;');
     79              this.xhr.send(this.param(parameters));    
     80              this.xhr.onreadystatechange=function(){
     81                  if(yizhi_ajax.xhr.readyState ==4){
     82                      if(this.showMsg){
     83                          alert("發送完畢");
     84                      }

     85                      if(yizhi_ajax.xhr.status==200){
     86                          if(this.showMsg){
     87                              alert("請求成功并完成!");
     88                          }

     89                          callBack(yizhi_ajax.xhr);
     90                          yizhi_ajax.xhr=false;
     91                      }
    else{
     92                          if(this.showMsg){
     93                              alert("服務端返回狀態" + yizhi_ajax.xhr.statusText);
     94                          }

     95                      }

     96                  }

     97              }

     98          }

     99      }

    100          
    101  }

    index.jsp
     1
     2
     3<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     4<html>
     5  <head>
     6    
     7    <title>無聊就要涮鍋子</title>
     8    <meta http-equiv="content-type" content="text/html;charset=UTF-8">
     9    <meta http-equiv="pragma" content="no-cache">
    10    <meta http-equiv="cache-control" content="no-cache">
    11    <meta http-equiv="expires" content="0">    
    12  </head>
    13  <script type="text/javascript" src="js/yizhi_ajax.js"></script>
    14  <script type="text/javascript">    
    15       var check=function(){
    16      var param={
    17          name:document.getElementById("uname").value
    18      }

    19      yizhi_ajax.send("http://127.0.0.1:8080/test/servlet/test",param,function(xhr)
    20          {
    21              try{
    22                  var response =eval("(" + xhr.responseText + ")"); 
    23              if(response.is=="yes"){
    24                  alert("果然你是死鍋子,唰,刷刷刷!!!!!!!!!!!!!唰鍋子就是爽 O(∩_∩)O哈哈~");
    25              }
    else{
    26                  alert("你真TMD太令我失望了。。。╮(╯▽╰)╭哎。。。");
    27              }

    28              }
    catch(e){
    29              }

    30          }

    31      );
    32      
    33  }

    34  
    </script>
    35  <body>
    36    請輸入您的姓名:<input id="uname">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    37        <input type="button" value="你是死鍋子嗎?" onclick="check();">
    38       
    39  </body>
    40</html>
    41

    servlet文件(test.java  URL映射為:/servlet/test   本例子用了json2.3,如需相關jar包請自行搜索過著去官網下載)
    package com.yz.test;

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.net.URLDecoder;
    import java.util.HashMap;
    import java.util.Map;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import net.sf.json.JSONObject;

    public class test extends HttpServlet {


        
    public void doGet(HttpServletRequest request, HttpServletResponse response)
                
    throws ServletException, IOException {

            doPost(request,response);
        }


        
    public void doPost(HttpServletRequest request, HttpServletResponse response)
                
    throws ServletException, IOException {
            request.setCharacterEncoding(
    "UTF-8");
            response.setContentType(
    "text/html;charset=UTF-8");
            PrintWriter out 
    = response.getWriter();
            
    try {
                Map map 
    = new HashMap();
                
    if("郭照友".equals(URLDecoder.decode(request.getParameter("name")))){
                    map.put(
    "is","yes");
                }
    else{
                    map.put(
    "is","no");
                }

                JSONObject json 
    = JSONObject.fromObject(map);
                out.println(json);
            }
     catch (Exception e) {
            }

            out.flush();
            out.close();
        }

    }

    Feedback

    # re: 小毅原創之---無聊時心血來潮純手寫ajax請求封裝類(ajax請求參數支持JSON對象)  回復  更多評論   

    2010-10-19 18:07 by caikeyter
    太爛了

    posts - 0, comments - 21, trackbacks - 0, articles - 101

    Copyright © H2O

    主站蜘蛛池模板: 亚洲国产综合无码一区| 亚洲一区二区三区四区视频| 日韩免费人妻AV无码专区蜜桃| 亚洲精品自拍视频| 色视频色露露永久免费观看| av电影在线免费看| 亚洲成a人片在线观看播放| 国产男女猛烈无遮挡免费视频网站| 久久国产一片免费观看| 亚洲欧洲日韩极速播放| 中文字幕亚洲日韩无线码| 最近中文字幕免费完整| 亚洲AV成人潮喷综合网| 国产又黄又爽又刺激的免费网址| 一个人晚上在线观看的免费视频| 亚洲精品视频在线观看免费| 国产在线19禁免费观看| 91人人区免费区人人| 免费国产va视频永久在线观看| 亚洲综合一区二区国产精品| 亚洲av无码成人精品区在线播放| 免费国产污网站在线观看15| 直接进入免费看黄的网站| 激情内射亚洲一区二区三区| 免费A级毛片在线播放不收费| 2021精品国产品免费观看| 一边摸一边爽一边叫床免费视频| 亚洲国产成a人v在线| 亚洲男人天堂2020| 卡一卡二卡三在线入口免费| 美女在线视频观看影院免费天天看| 亚洲av永久中文无码精品| 亚洲av无码乱码国产精品fc2| 免费国产人做人视频在线观看| 99re免费视频| 国产午夜精品理论片免费观看 | 亚洲天堂2016| 亚洲av无码乱码国产精品fc2| 亚洲综合色成在线播放| 免费a级毛片视频| 中文字幕亚洲综合久久菠萝蜜|