You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Ajax调用结果的条件逻辑:消息展示与函数调用问题咨询

实现Ajax响应后的消息展示与函数调用逻辑

根据需求调整代码逻辑,让Ajax请求CheckDatabase返回不同响应时执行对应的操作:

修改后的完整代码

function CheckDatabaseInfo() {
    $.ajax({
        url: 'CheckDatabase',
        method: 'GET',
        success: function (res) {
            DBResult(res);
        },
        error: function (jqXHR, exception) {
            $('#message').html('连接服务器时发生未知错误');
        }
    });
}


function CheckCompanyProfile() {
    $.ajax({
        url: 'CheckCompanyProfile',
        method: 'GET',
        success: function (res) {
            if (res === "found") {
                // 跳转到登录页
                console.log(res);
                window.location.replace("UserLogin");
            } else if (res === "conflict") {
                // 处理冲突档案
                console.log(res);
                $('#message').html('存在冲突的公司档案。<br>点击 <a href="#">这里</a> 解决');
            } else {
                // 创建新公司档案
                console.log("创建档案");
                $('#message').html('未找到您的公司。<br>点击 <a href="#">这里</a> 创建');
            }
        },
        error: function (jqXHR, exception) {
            $('#message').html('连接服务器时发生未知错误');
        }
    });
}

function DBResult(x) {
    const val = x.trim(); // 去除响应首尾空格,避免匹配失效
    if (val === "yes") {
        console.log("数据库检查通过");
        $('#message').html('已找到数据库');
        CheckCompanyProfile(); // 触发公司档案检查逻辑
    } else if (val === "no") {
        console.log("未找到系统数据库");
        $('#message').html('未找到系统数据库');
    } else if (val === "error") {
        console.log('数据库检查出错');
        $('#message').html('数据库检查过程中发生错误');
    } else {
        console.log("检测到未知错误");
        $('#message').html('发生未知错误,请稍后重试');
    }
}

关键改动说明

  1. 核心逻辑实现:在DBResult函数的yes响应分支中,新增CheckCompanyProfile()调用,满足"数据库检查通过后执行公司档案检查"的需求;
  2. 精准响应处理:新增error响应的专属分支,明确区分后端返回的错误类型,避免和未知响应混淆;
  3. 细节优化:
    • 给响应内容添加trim()处理,规避后端返回字符串带首尾空格导致的匹配失败问题;
    • 把原消息中的换行符替换为<br>,确保HTML页面中换行显示正常;
    • 修正原代码中console.log的拼写错误;
    • 统一所有Ajax错误回调的提示文本,保持用户体验一致性。

内容的提问来源于stack exchange,提问作者Muinde J Kivai

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 07:45:35