1. 程式人生 > >Ajax非同步載入web網頁資料

Ajax非同步載入web網頁資料

html程式碼

   <select id="cinemaName" name="release.cinema.cinema_name"  class="select w50" style="width:250px;">  //javascript程式碼通過id名稱寫
            <option value="" selected="selected">請選擇影院</option>
            <s:iterator  value="cinemas" id="m">
                <option value
="<s:property value="#m.cinema_name"/>
" ><s:property value="#m.cinema_name"/></option> </s:iterator> </select>

javascript程式碼

<script type="text/javascript">
        $(document).ready(function(){ 
        $('#cinemaName').change(function(){
var val=$(this).children('option:selected').val(); val=$.trim(val); var url="releaseAction_getCinemaInfo.action"; alert($(this).children('option:selected').val()); var p1=$(this).children('option:selected').val();//這就是selected的值 var
args={"cinema.cinema_name":val,"time":new Date()}; $.post(url,args,function(data){ //alert(data); $("#input1").val(data.cinema_id); $("#input2").val(data.cinema_address); },"json"); }) $('#movieName').change(function(){ var val=$(this).children('option:selected').val();//這就是selected的值 var p1=$(this).children('option:selected').val();//這就是selected的值 val=$.trim(val); var url="releaseAction_getMovieInfo.action"; alert($(this).children('option:selected').val()); var args={"movie.movie_name":val,"time":new Date()}; $.post(url,args,function(data){ var obj = eval(data); // alert("主演是:"+data.movie_role); $("#input3").val(data.movie_id); $("#input4").val(data.movie_role); $("#input5").val(data.movie_director); $("#input6").val(data.movie_length); },"json"); }) }) //window.location.href="xx.php?param1="+p1+"¶m2="+p2+"";//頁面跳轉並傳參
</script>

Action類程式碼

    public void  getCinemaInfo() throws Exception{
        jsonObject=new JSONObject();

        if (cinemaService.getCinemaByCinemaName(cinema).get(0)!=null) {
              cinema = cinemaService.getCinemaByCinemaName(cinema).get(0);
              HttpServletResponse response=ServletActionContext.getResponse();
              response.setContentType("text/html;charset=utf-8");
              PrintWriter out = response.getWriter();  

              jsonObject.put("cinema_id", cinema.getCinema_id());
              jsonObject.put("cinema_address", cinema.getCinema_address());
              out.println(jsonObject);  
              out.flush();  
              out.close();  
        }
   }

資料以json陣列的形式互動(ajax非同步通訊)

後臺程式碼

   public void  getCinemaInfobySelect() throws Exception{
            System.out.println("列印js 介面傳來的選擇日期引數"+date_selected);
            //這裡 的6是影院id,前面的頁面提供,先寫死 的

            if (releases.size()>0) {
                jsonArray =JSONArray.fromObject(releases);
                //列印訂單資訊
                for(MovieRelease m:releases)
                {
                    System.out.println(m);
                }
                 HttpServletResponse response=ServletActionContext.getResponse();
                 response.setContentType("text/html;charset=utf-8");
                 PrintWriter out = response.getWriter();  
                 System.out.println("列印傳給介面的值:"+jsonArray.toString());
                 System.out.println("列印傳給介面的值:"+jsonArray);
                 out.println(jsonArray);  
                 out.flush();  
                 out.close();  
            }else {
                System.out.println("該影片沒有查詢到排片資訊!");
            }
       }

js程式碼

    $('#date_select').change(function(){ 
        var val=$(this).children('option:selected').val();
            val=$.trim(val);
        var url="orderAction_getCinemaInfobySelect.action";
        alert($(this).children('option:selected').val()); 
        var p1=$(this).children('option:selected').val();//這就是selected的值 
        var args={"date_selected":val,"time":new Date()};
        $.post(url,args,function(data){
//            alert(data);
              $.each(data, function(index, val) {
                  alert("列印查出來的排片id"+val.release_id);
                  });
        },"json"); 
    })