0

我需要根据字段codigo_caso的模糊程度在 JSP 页面中自动填写表单。我将一个简单的类似 json 的字符串传递给另一个 JSP,该 JSP 正在检索完成表单所需的数据。

我已经测试了该页面,它正在调用第二个 JSP,它从数据库中获取正确的数据并用它构建一个 JSONObject。

问题是它似乎没有将 JSON 返回到第一个 JSP 页面,并且成功函数内的代码没有执行。[如果可能的话,我需要让它在 jsp 中工作,而不是在 servlet 中]

这是脚本 - 第一个 jsp

<script type="text/javascript">
  $("#codigo_caso").blur(function() {
    var cod = $(this).val();
    var dataString1 = {"codigo": cod};
    $.ajax({
      type: "GET",
      url: "otros/codigoCasoDependienteNuevaTarea.jsp",
      datatype: "json",
      contentType: "application/json; charset=utf-8",
      data: dataString1,
      cache: false,
      success: function(response) {
        alert ( response );
        var jsontest = eval(response);
        $(".linea_equipo").val(jsontest("linea_equipo"));
        $("#selectArea").filter(function() {
            return $(this).jsontest("id_area") === jsontest("area");
        }).prop('selected', true);
        //("#selectArea").val(area);
        $(".listaCentros").jsontest("nombre_centro");
        $("#listaRolNuevaTarea").filter(function() {
            return $(this).jsontest("id_rol") === jsontest("rol");
        }).prop('selected', true);
        //("#listaRolNuevaTarea").val(rol);
      }
    });
  });

这是从数据库中检索数据的 JSP [意图返回 JSON]

<%@page import="java.sql.*"%>
<%@page import="clases.*"%>
<%@page import="org.json.simple.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<%
String data1 = "", linea = "", centro = "", error = "No existe el caso indicado", cadenaOut = "";
int area, rol;
Connection conex = (Connection) session.getAttribute("conexion");
Statement st = conex.createStatement();
String sql = "";
String cod = request.getParameter("codigo").toString();
JSONObject j = new JSONObject();
if (cod != null && !cod.isEmpty() && !cod.equals("0")) {
    sql = "SELECT t1.linea_equipo,t1.id_rol, t2.id_area,t2.nombre_centro "
            + " FROM gen_casos_prisma t1 LEFT OUTER JOIN gen_centros t2 ON "
            + " t1.id_centro = t2.id_centro "
            + " WHERE "
            + " t1.CODIGO_CASO =  " + cod;
    ResultSet rs = st.executeQuery(sql);
    rs.beforeFirst();
    if (rs.next()) {
        linea = rs.getString("linea_equipo");
        area = rs.getInt("id_area");
        centro = rs.getString("nombre_centro");
        rol = rs.getInt("id_rol");
        j.put("linea_equipo", linea);
        j.put("id_area", area+"");
        j.put("nombre_centro", centro);
        j.put("id_rol", rol+"");
        System.out.println(j.toString());
    } else {
        /* return variable error */
    }
}
response.setContentType("application/json");
response.getWriter().print(j);
out.flush();
//out.print(j.toJSONString());
%>
</html>

当代码打印 json 时,[第二个 jsp,几乎在最后] 它向我展示了这样的内容:

  • {"id_area":"268435465","linea_equipo":"MODULAR xxx","nombre_centro":"center xxx","id_rol":"2"}
4

1 回答 1

0

If you want the javascript to receive only the json, remove the

<html> 

and

</html> 

tags from the JSP so it will return only the json.

Obviously since you called

 response.setContentType("application/json"); 

it doesn't make sense to be using html tags in the output.

于 2013-10-15T13:29:45.430 回答