如何在struts中使用Ajax json 1.2使用父子页面的Action

时间:2013-04-03 05:12:19

标签: java javascript jquery json

如何在struts中使用Ajax json 1.2使用父子页面的操作

我在Action页面中使用return null。但它不会检索数据。请尽可能在今天晚上给我一个需要的答案。提前谢谢

这是我的java脚本编码

(文档)$             。准备(                     function(){                     警报(“内部1”);                         $(function(){

                        $("select#feedTypeValue")
                                .change(
                                        function() {
                            alert("inside 2");
                                            $   

                                                    .ajax({
                                                        type : 'GET',
                                                        url : 'IndexReviewAction.do?menuIndex=3',
                                                        data : {

                                                            feedTypeValue : $(
                                                                    this)
                                                                    .val()
                                                        },
                                                        dataType : 'JSON',
                                                        cache : false,
                                                        success : function(
                                                                j) {

                                                            var options = '';

                                                            for ( var k = 0;    k < j.length; k++) {
                                                                options += '<option value="' + j[k] + '">'
                                                                        + j[k]
                                                                        + '</option>';

                                                            }

警告(“in for”);                                                                 $(                                                                         “选择#listValue”)                                                                         html的(                                                                                 选项);

                                                        }
                                                    });
                                        });

                    });
                });

这是我的行动页面编码

String feedType = request.getParameter(“feedTypeValue”);

            if(menuIndex!=null && menuIndex.equalsIgnoreCase("3") )
            {

ArrayList listValue = new ArrayList(); if(feedType!= null){

                if(feedType.equals("ADDA") ){

            listValue.add("ASD");
            listValue.add("XSD");


            }else{

                listValue.add("QWE");
                listValue.add("AZX");

            }

             String jsonResult = new flexjson.JSONSerializer()
             .serialize(listValue);
            response.setContentType("text/javascript");
            response.getWriter().write(jsonResult);
          //return Json(listValue,JsonRequestBehavior.AllowGet);

            return null;
            }

            }

1 个答案:

答案 0 :(得分:1)

试试这个......

<script>
    $(document).ready(function() {
        $("#feedTypeValue").change(function() {
            feedTypeValue= $(this).val(); 
            var params = 'menuIndex=3&feedTypeValue='+feedTypeValue;  
            $.ajax({
                type: "POST",
                async:false,
                url: "IndexReviewAction.do",
                data: params,
                cache : false,
                success: function(result){
                    document.getElementById('sample').innerHTML=result; 
                }
            });
        });
     });

 </script>

 <select id="feedTypeValue">
     <option value="one">one</option>
     <option value="ADDA">ADDA</option>
 </select>
 <select id="sample">
 </select>

行动类

 public ActionForward IndexReviewAction(ActionMapping mapping,  
        ActionForm form,  
        HttpServletRequest request,  
        HttpServletResponse response)  
        throws Exception {
    String feedType=request.getParameter("feedTypeValue");
    PrintWriter out = null;
    String option="";   
     if(request.getParameter("menuIndex")!=null && 
    request.getParameter("menuIndex").equalsIgnoreCase("3") )
     {
         if(feedType.equals("ADDA") ){

     option+="<option value='ASD'>ASD</option><option value='XSD'>XSD</option>";

                }else{
    option+="<option value='QWE'>QWE</option><option value='AZX'>AZX</option>";

                }
     }
        out=response.getWriter();
        out.println(option);
        System.out.println(option);

        return null;



}