Dasfv
Dasfv
采纳率50%
2020-04-27 02:18

刚学习,麻烦大神看下,我的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条回答

  • w172087242 little_how 1年前

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

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

    希望对你有帮助...

    点赞 1 评论 复制链接分享