东西是好东西就是有点费头发 2020-04-27 02:18 采纳率: 0%
浏览 167
已采纳

刚学习,麻烦大神看下,我的ajax异步servlet回调函数得不到值

$(function() {// 使用AJAX获取列表数据

$(".views").on("click",function(){
    var sickId=$(this);
    $.ajax({
        url : "sickInfoServlet",
        type : "GET",
        data : {sickId:sickId.attr("ide")},
        dataType : "json",
        success : function(result){
                alert(result.sickId);
                $("#v_sickId").val(result.sickId);

                $("#v_hospitalId").val(result.hospitalId);
                $("#v_sickBedId").val(result.sickBedId);
                $("#v_doctorId").val(result.doctorId);
                $("#v_nurseId").val(result.nurseId);
                $("#v_sickName").val(result.sickName);
                $("#v_sickSex").val(result.sickSex);
                $("#v_sickAge").val(result.sickAge);
                $("#v_inresult").val(result.inresult);
                if(result.outresult==null){
                    $("#v_outresult").val("未出院");
                }else{
                    $("#v_outresult").val(result.outresult);
                }
                $("#v_sickAddress").val(result.sickAddress);
                $("#v_sickPhone").val(result.sickPhone);
                $("#v_leaveHospital").val(result.leaveHospital);
                $("#v_cash").val(result.cash);
        },
        error : function(xmlhttprequest) {
            alert("出现未知错误,查看失败");
            alert(xmlhttprequest.status);
            alert(xmlhttprequest.readyState);
    }
});

});
});

// servlet
package com.hospital.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hospital.po.InHospital;
import com.hospital.profesachieve.SickInfoServiceImpl;

public class SickInfoServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    // 编码集处理
    req.setCharacterEncoding("UTF-8");
    resp.setContentType("application/json;charset=utf-8");

    // 接收前端页面的消息
    String sickId=req.getParameter("sickId");
    System.out.println("传入的值sickId:"+sickId);
    PrintWriter out=resp.getWriter();

    // 处理SickInfo
    InHospital  i= new SickInfoServiceImpl().view(sickId);

    //将上述对象集合封装成json格式进行输出
    StringBuffer Json=new StringBuffer("[");
        Json.append("{\"sickId\":\""+i.getSickId()+"\",");
        Json.append("\"hospitalId\":\""+i.getHospitalId()+"\",");
        Json.append("\"sickBedId\":\""+i.getSickBedId()+"\",");
        Json.append("\"doctorId\":\""+i.getDoctorId()+"\",");
        Json.append("\"nurseId\":\""+i.getNurseId()+"\",");
        Json.append("\"sickName\":\""+i.getSickName()+"\",");
        Json.append("\"sickSex\":\""+i.getSickSex()+"\",");
        Json.append("\"sickAge\":\""+i.getSickAge()+"\",");
        Json.append("\"inData\":\""+i.getInData()+"\",");
        Json.append("\"outData\":\""+i.getOutData()+"\",");
        Json.append("\"sickAddress\":\""+i.getSickAddress()+"\",");
        Json.append("\"sickPhone\":\""+i.getSickPhone()+"\",");
        Json.append("\"leaveHospital\":\""+i.getLeaveHospital()+"\",");
        Json.append("\"cash\":\""+i.getCash()+"\"}");
        Json.append("]");
    String res =new String(Json);

    System.out.println("生成的res:"+res);

    out.print(res);
    out.close();

}

// 前端

患者编号: 医院编号: 患者名字:

患者性别: 患者年龄: 住院时间:

出院时间: 患者地址: 联系电话:

离院备注: 住院押金:

点击查询后可以正常执行servlet生成json格式数据生成的res:[{"sickId":"1336565555","hospitalId":"741852963","sickBedId":"105003","doctorId":"20307","nurseId":"20102","sickName":"闫晓丽","sickSex":"女","sickAge":"36","inData":"2020-04-01","outData":"null","sickAddress":"陕西","sickPhone":"18062542369","leaveHospital":"null","cash":"200.0"}],但是到sucess中alert弹窗获取到的值为undefined,前端页面也没有加载出来值,浏览器未报错。
  • 写回答

1条回答 默认 最新

  • little_how 2020-04-27 10:15
    关注

    1.把你的servlet返回的[和]去掉,也就是stringbuffer的第一个字符和最后一个字符;
    因为这样出来的是json数组,而你后台是json接收;

    2.如果不想改servlet,那么你就需要在你得回调函数那里alert的上面加上一句result = result[0]取出json元素即可;

    希望对你有帮助...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 单纯型python实现编译报错
  • ¥15 c++2013读写oracle
  • ¥15 c++ gmssl sm2验签demo
  • ¥15 关于模的完全剩余系(关键词-数学方法)
  • ¥15 有没有人懂这个博图程序怎么写,还要跟SFB连接,真的不会,求帮助
  • ¥15 PVE8.2.7无法成功使用a5000的vGPU,什么原因
  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音