【问题标题】:not getting the JSON(Object/Array) from servlet to JSP page没有从 servlet 获取 JSON(Object/Array) 到 JSP 页面
【发布时间】:2015-07-01 09:22:23
【问题描述】:

我正在尝试将值从 servlet 获取到 JSP 页面,但我无法做到。 servlet 页面

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String region = request.getParameter("region");
    String secretKey = request.getParameter("secret_key");
    String accessKey = request.getParameter("access_key");
    String VPC = request.getParameter("vpc");   
    LOG.info("region is  "+region);
    if(VPC == null){
        List<String> vpcs = RunEc2Command.getVPCForUSer(accessKey, secretKey, region);
        if(vpcs != null && vpcs.size() > 0) {
            LOG.info("Number of VPCs: "+vpcs.size());       
            response.setContentType("application/json");
            PrintWriter out = response.getWriter();
            JSONArray jsonAraay = new JSONArray(vpcs);
            LOG.info(jsonAraay);
            out.print(jsonAraay);
            out.flush();
        }
    }
}

在 index.jsp 页面中,我试图获取 JSONArray 值

function getSubnets(){
var p = document.getElementById("vpc");

   var output2 = $.ajax({
     type: 'POST',
     url: 'http://localhost:8066/VMMigratorNew/ec2util',
     data: {
            "vpc":p.value
            },
    dataType:'json',
     success: function(data) {
                        $.each(data.jsaonArray, function(index){
                            var selectBox = "<select>"
                            $.each(data.jsonArray[index], function(key , value){
                                selectBox+="<option>"+key + "& value" + value + "</option>";

                            });
                            selectbox+="</select>";
                            $('vpc').html(selectBox);
                        });
            //return output2.responseText;
     }
});

} 下拉框没有被填充,下拉框的代码是

<select id="vpc" name="vpc" class="form-control" placeholder="Choose VPC" >
                                        <option value="" default selected>Select VPC</option>
                                     </select> 

我是 JSON 新手,提前感谢您的帮助。

【问题讨论】:

  • 你在浏览器中调试过吗?服务器真的被调用了吗?尝试将少量 alert(...)console.log(...) 调用放入您的 success 处理程序中。
  • servlet 被调用,并且从 RunEc2Command 类中我也得到了 servlet 页面的值,但我无法将相同的值传递给 jsp 页面。
  • 如果登录为console.log(data, $.isPlainObject(data)),你会在成功块中得到什么?
  • 我在控制台中没有得到任何东西,你能帮我解决这个问题吗??

标签: javascript java ajax json servlets


【解决方案1】:

试试这个....

 var selectBox=""
    $.each(data.jsonArray[index], function(key , value){
                  selectBox+="<option>"+key + "& value" + value + "</option>";
                 });
    $('vpc').append(selectBox);

【讨论】:

  • 你检查过浏览器控制台吗? ...请检查您的浏览器控制台并检查是否有任何错误通知.....
  • 我检查了,浏览器控制台没有错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-24
  • 2016-05-17
  • 2017-03-26
  • 1970-01-01
  • 1970-01-01
  • 2010-11-12
相关资源
最近更新 更多