vjame

          優(yōu)化代碼是無止境的
          隨筆 - 65, 文章 - 9, 評(píng)論 - 26, 引用 - 0
          數(shù)據(jù)加載中……

          js處理服務(wù)器返回的對(duì)象

          格式一:
          {
          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 閱讀(440) 評(píng)論(0)  編輯  收藏 所屬分類: Java Web

          主站蜘蛛池模板: 中阳县| 乾安县| 马鞍山市| 监利县| 太谷县| 南充市| 六安市| 筠连县| 花莲县| 象州县| 天镇县| 徐闻县| 青铜峡市| 彩票| 右玉县| 兴和县| 长汀县| 霞浦县| 永嘉县| 大石桥市| 东乌珠穆沁旗| 静海县| 长子县| 台北县| 新晃| 县级市| 蒲城县| 濮阳市| 崇州市| 大安市| 安康市| 高青县| 沧州市| 文登市| 绥化市| 桂阳县| 定结县| 抚顺市| 双城市| 海宁市| 玉环县|