vjame

          優化代碼是無止境的
          隨筆 - 65, 文章 - 9, 評論 - 26, 引用 - 0
          數據加載中……

          js處理服務器返回的對象

          格式一:
          {
          firstname:"Bruce",
          lastname:"Perry",
          gender:"M",
          country:"USA"
          }

          代碼片段:

          function handleJson(  ){
              
          if(request.readyState == 4){
                  
          if(request.status == 200){
                      
          var resp =  request.responseText;
                      
          var func = new Function("return "+resp);
                      
          var objt = func(  );
                      
          var div = document.getElementById("json");
                      stylizeDiv(resp,div);
                      div 
          = document.getElementById("props");
                      div.innerHTML
          ="<h4>In object form</h4>"+
                                    
          "<h5>Properties</h5>firstname= "+
                                    objt.firstname 
          +"<br />lastname="+
                                    objt.lastname
          + "<br />gender="+
                                    objt.gender
          + "<br />country="+
                                    objt.country;
                  } 
          else {
                      alert(
          "A problem occurred with communicating between "+
                           
          "the XMLHttpRequest object and the server program.");
                  }
              }
          //end outer if
          }

           


          格式二:

          [
           "basketball","football"
          ]


          代碼片段:

          //event handler for XMLHttpRequest
          function handleResponse(){
              
          try{
                  
          if(request.readyState == 4){
                      
          if(request.status == 200){
                          
          var resp =  request.responseText;
                        
                          
          if(resp != null){
                              
          //return value is a JSON array
                              var objt = eval(resp);
                              
                              createChecks(objt);
                          }

                      }
           else {
                          
          //request.status is 503  if the application isn't available;
                          //500 if the application has a bug
                          alert(
                                  
          "A problem occurred with communicating between"+
                                  
          " the XMLHttpRequest object and the server program.");
                      }

                  }
          //end outer if
              }
           catch (err)   {
                  alert(
          "It does not appear that the server "+
                        
          "is available for this application. Please"+
                        
          " try again very soon. \nError: "+err.message);

              }

          }




           

          posted on 2011-06-08 11:15 lanjh 閱讀(426) 評論(0)  編輯  收藏 所屬分類: Java Web

          主站蜘蛛池模板: 博客| 桂阳县| 班玛县| 原平市| 临邑县| 隆化县| 红安县| 五台县| 平阴县| 墨江| 太仓市| 武陟县| 达日县| 关岭| 桓台县| 怀安县| 府谷县| 玉门市| 炉霍县| 长寿区| 封丘县| 宜城市| 锡林郭勒盟| 红原县| 黔东| 扎鲁特旗| 囊谦县| 河源市| 香港 | 祁东县| 施甸县| 贺州市| 达拉特旗| 深圳市| 叶城县| 勃利县| 英山县| 古蔺县| 永吉县| 鄂托克旗| 印江|