如何使用jquery ajax以JSON格式从服务器获取json响应

时间:2016-07-11 09:57:21

标签: java jquery json ajax

我正在解析错误

Ajax

$.ajax({
                    type: "POST",
                    url: "getStagingImrReport.do",
                    data: dataString,
                    dataType: "JSON", 
                    //if received a response from the server
                    success: function( data, textStatus, jqXHR) {  //our country code was correct so we have some information to display  
                        alert(data);
                      //$("#ajaxResponse").html("<div><b>Country code in Invalid!</b></div>"); 
                    }, 
                    //If there was no resonse from the server
                    error: function(jqXHR, textStatus, errorThrown){
                        alert("Something really bad happened " + textStatus);
                          //$("#ajaxResponse").html(jqXHR.responseText);
                    }, 
                    //capture the request before it was sent to server
                    beforeSend: function(jqXHR, settings){
                        //adding some Dummy data to the request
                        settings.data += "&dummyData=whatever";
                        //disable the button until we get the response
                        $('#myButton').attr("disabled", true);
                    }, 
                    //this is called after the response or error functions are finsihed
                    //so that we can take some action
                    complete: function(jqXHR, textStatus){
                        //enable the button 
                        $('#myButton').attr("disabled", false);
                    } 
                }); 

控制器

public void getStagingImrReport(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        try {
            // String policyType = request.getParameter("policyType");
            List<String> drop = new ArrayList<String>();
            response.setContentType("application/json");
            response.setHeader("Cache-Control", "no-cache");
            response.setHeader("Pragma", "no-cache");
            final ILoadMgr iLoadMgrRef = (ILoadMgr) getContext().getBean(
                    IConstants.LOAD); 
            List<PTypeVO> listVO = iLoadMgrRef
                    .getPolicyTypes(IConstants.MFFILTER);
            for (PTypeVOpolicy : listVO) {
                drop.add(policy.getPolicyTypeName()); 
            }
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("jsonVal", drop);
            PrintWriter out = response.getWriter(); 
            System.out.println(" XXXX--------"+drop);

            out.println(drop); 
            out.flush();
        } catch (Exception exp) {
            Logging.error(
                    "[LoadController.getpolTypesAjaxDropdown] Exception occured ",
                    exp);
        }
    }

sysOut控制台上的输出

 //----XXXX--------[CHIP, County Health Program, Disability, Discount Card, Imputed, Indian Tribe, Mail Order, Medicaid MCO, No Coverage, Other, Rehabilitation, State]

和Jquery警报消息显示:

"something really bad happened parse error"

1 个答案:

答案 0 :(得分:0)

您的返回类型不是JSON ..列表或数组字符串,更改您的数据类型或更改您的返回类型