无法通过jquery从控制器到jsp获得响应

时间:2016-03-11 08:24:21

标签: jquery jsp spring-mvc httpresponse

我从dropdown(oncgange函数)向控制器发送一个id。它工作正常。现在在控制器中查询数据库后,结果存储在arrylist中。我已经给了返回类型的arrylist。没问题或有任何错误。请帮忙。我只想显示另一个基于下拉的选定值的下拉列表。 假设我选择了Fruit,所以我想在jsp中填充它。

<option value="mango">Mango</option>
<option value="pineapple">Pineapple</option>

JSP

         <script>
            function getScode()
            {
                var code = document.getElementById('code'),
                    code = code.value;
                jQuery.ajax({
                    type: 'POST',
                    url: 'scode.htm',
                    data: {
                        code: code
                    },
                    success: function (html) {
                        $('#scode_data').html(html).show();
                    }
                });
                return false;
            }
        </script>

          Select Genre
            <select name="code" id="code"  onchange="getScode();">        
            <option value="1">Fruit</option>
            <option value="2">Game</option>              
            </select>
          Under that
         <div id="scode_data"></div>

控制器

@RequestMapping(value="/scode", method = RequestMethod.POST)
    public @ResponseBody ArrayList getScode(ModelMap modelMap, @RequestParam(value = "code", required = false) String code_val, HttpServletRequest request, HttpServletResponse response) 
throws ClassNotFoundException { 

        SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
        dataSource = new dbconnection.connection().getConnection(dataSource);
        JdbcTemplate jt = new JdbcTemplate(dataSource);

        String sql = "SELECT * FROM cat WHERE fcode='"+code_val+"'";
        srs = jt.queryForRowSet(sql);
        while (srs.next()) {
            pojo obj2 = new pojo();
            obj2.setScode(srs.getString("fcode"));
            obj2.setSname(srs.getString("fname"));
            arraylist.add(obj2);
        }

         return arraylist;
    }

Image link of what i am getting

1 个答案:

答案 0 :(得分:0)

现在您要将json字符串转换为选项节点。下面的代码可以完成工作:

success : function(result) {
    var parsed = JSON.parse(result);

            var b = "";
            for(var x = 0; x<parsed.length ; x++){
                b = "<option value = '"+parsed["scode"]>+"'>"
                b = parsed["sname"] + "</option>";
                $("#__id of your select_").append(b);
                b = "";
            }