AJAX - 服务器端正常,在客户端返回错误

时间:2013-11-28 15:39:44

标签: java javascript ajax spring-mvc

我有一个AJAX的问题:在服务器端我得到一个结果但在客户端(javascript)它返回错误。

服务器端:

@RequestMapping(value = "/nrConturi", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public List<Cerere> setNrConturi(@RequestParam(value = "jud", defaultValue = "") String jud,
                                 @RequestParam(value = "aplic", defaultValue = "") String aplic,
                                 @RequestParam(value = "oper", defaultValue = "") String oper) {
    LOG.debug(">>>>> {}", jud);
    LOG.debug(">>>>> {}", aplic);
    LOG.debug(">>>>> {}", oper);
    String numeJud = scoateNumeJudetDupaCod(jud);

    List<Cerere> list = null;
    String sql = "SELECT * FROM V_COM_DETALII_CERERE CER, COM_CERERE_APLICATIE APP, OPERATORECONOMIC OP, APLICATIE_PORTAL_EXTERN AP " +
            "WHERE APP.FK_COM_CERERE_OP = CER.ID AND CER.COD_FISCAL = OP.CODFISCAL AND APP.FK_APLICATIE = AP.ID AND CER.STATUS=2";

    if (jud != "" && aplic != "" && oper != "") {
        sql += " AND CER.JUDET = '" + numeJud + "'";
        sql += " AND APP.FK_APLICATIE = " + aplic;
        sql += " AND OP.IDOPERATORECONOMIC = " + oper;
    }
    sql += " ORDER BY CER.ID";

    list = jdbcTemplate.query(sql, CerereRowMapper.INSTANCE);
    //I get here a size...xx list...
    return list;
}

客户方:

    var filtramDupaAplicatie = $('#aplicatie_check').is(':checked');
    var filtramDupaJudet = $('#judet_check').is(':checked');
    var filtramDupaOperator = $('#opEc_check').is(':checked');
    var aplicatie = "";
    var judet = "";
    var operator = "";
    if (filtramDupaAplicatie)
        aplicatie = $("#aplicatie").val();
    if (filtramDupaJudet)
        judet = $("#judet").val();
    if (filtramDupaOperator)
        operator = $("#operatorEconomic").val();

    $.ajax({
        url: contextPath + '/nrConturi',
        data: {
            jud: judet,
            aplic: aplicatie,
            oper: operator
        },
        encoding: "UTF-8",
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        method: 'GET',
        success: function (list) {
            $('#nrConturi').val(list.length);
        },
        error: function(e){
            displayError(JSON.stringify(e));
        }
    });
  • 返回:“readyState”:4,“responseText”:“Apache Tomcat / 7.0.39 - 错误报告.................. HTTP状态500 - 请求处理失败;嵌套异常是java.lang.IllegalStateException:已经为此响应调用了getOutputStream()

    类型异常报告

    消息请求处理失败;嵌套异常是java.lang.IllegalStateException:已为此响应调用了getOutputStream()

    描述服务器遇到内部错误,导致无法完成此请求。

    异常 org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是java.lang.IllegalStateException:已经为此响应调用了getOutputStream() ñ...........

1 个答案:

答案 0 :(得分:0)

好的,我解决了这个问题:

    @RequestMapping(value = "/nrConturi", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public List<Cerere> setNrConturi(@RequestParam(value = "jud", defaultValue = "") String jud,
                                     @RequestParam(value = "aplic", defaultValue = "") String aplic,
                                     @RequestParam(value = "oper", defaultValue = "") String oper) {
        LOG.debug(">>>>> {}", jud);
        LOG.debug(">>>>> {}", aplic);
        LOG.debug(">>>>> {}", oper);

        List<Cerere> list = cerereRepository.nrConturi(jud, aplic, oper);
        return list;
}

并在cerereRepository中:

public List<Cerere> nrConturi(String jud, String aplic, String oper) {

        List<Cerere> list = null;
        String numeJud = scoateNumeJudetDupaCod(jud);

        StringBuilder queryBuilder = new StringBuilder("select c from DetaliiCerere c");

        if (!aplic.equals("")) {
            queryBuilder.append(" join c.aplicatii a where a.id in (");
            queryBuilder.append(aplic);
            queryBuilder.append(")");
        } else {
            queryBuilder.append(" where");
        }

        if (!jud.equals("")) {
            if (!aplic.equals(""))
                queryBuilder = queryBuilder
                        .append(" and");

            queryBuilder = queryBuilder
                    .append(" upper(c.judet)=upper('")
                    .append(numeJud)
                    .append("')");
        }


        if (!oper.equals("")) {
            if (!aplic.equals("") || !jud.equals("")) {
                queryBuilder = queryBuilder
                        .append(" and");
            }

            queryBuilder = queryBuilder
                    .append(" upper(c.codFiscal) like upper('%")
                    .append(oper)
                    .append("%')");
        }

        queryBuilder = queryBuilder
                .append(" and c.status='Cerere aprobata'");

        String jpQL = queryBuilder.toString();
        LOG.debug("JPQL >> ... {}", jpQL);

        Query query = entityManager.createQuery(jpQL);
        list = query.getResultList();

        return list;
    }

我在Javascript中得到了结果。 THX。