如何在Ajax服务器端调用中处理NULL响应:.done(函数(数据){..}从未激活过

时间:2018-03-20 17:01:09

标签: jquery json ajax

假设我从jQuery执行Ajax服务器端调用,例如

    $.ajax({
        url: "/myapp/fetchUser?username=" + username, 
        type : "get",
        dataType : "json",
        data : ''
    }).done(function(data) {
        console.log(data);
    });
}  

服务器端,在Java中,fetchUser映射从DB中获取可能存在或为NULL的JSON序列化UserT对象。如果对象存在,我成功地进入.done(function(data) {..}。但是如果返回的对象是NULL,我不要放入.done(function(data) ..)中的JS代码。

如何在jQuery中处理NULL响应?流程现在就停止了

1 个答案:

答案 0 :(得分:1)

pip install SQLAlchemy==1.1.15 --cert="C:\Users\myUser\certificates\my_ca-bundle.crt" 表示请求必须返回有效的json,即使它只是dataType : "json",。将端点更改为始终返回有效的json,或者从ajax请求中取出该选项并解析响应(如果存在)。

{}没有发生,因为jQuery正在尝试解析null,收到错误并转到错误回调。