简体   繁体   中英

AJAX - ok on server side, returns error on client side

I have a problem with an AJAX: In server side I get a result but in client side (javascript) it returns an error.

Server side:

@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;
}

Client side:

    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));
        }
    });
  • it returns: "readyState":4,"responseText":"Apache Tomcat/7.0.39 - Error report..................HTTP Status 500 - Request processing failed; nested exception is java.lang.IllegalStateException: getOutputStream() has already been called for this response

    Exception report 例外报告

    Request processing failed; 请求处理失败; nested exception is java.lang.IllegalStateException: getOutputStream() has already been called for this response

    The server encountered an internal error that prevented it from fulfilling this request. 服务器遇到内部错误,导致服务器无法满足此请求。

    org.springframework.web.util.NestedServletException: Request processing failed; :请求处理失败; nested exception is java.lang.IllegalStateException: getOutputStream() has already been called for this response\\n...........

Ok, I solved the problem:

    @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;
}

and in 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;
    }

and I've got the result in Javascript. Thx.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM