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

'/'应用程序Server Error 404错误:请求URL为/UsersPage/undefined求助

问题分析与解决

出现/UsersPage/undefined的404错误,核心原因是前端AJAX期望从后端拿到redirectUrl字段,但后端实际返回的不是包含该字段的JSON数据,导致response.redirectUrl为undefined,最终拼接出错误的跳转地址。

具体修复步骤

1. 修正控制器代码

当前控制器的UsersHome方法存在两个问题:没有接收前端传的chosenMood参数,且返回的是View而非AJAX需要的JSON数据。修改后的代码如下:

[HttpPost] // 明确标记为POST请求,和AJAX请求类型匹配
public ActionResult UsersHome(string chosenMood)    
{
    if (User.Identity.IsAuthenticated)
    {
        Session["ChosenMood"] = chosenMood; // 保存前端传递的参数
        var redirectUrl = Url.Action("UsersHome", "UsersPage");
        return Json(new { redirectUrl }, JsonRequestBehavior.AllowGet); // 返回包含跳转地址的JSON对象
    } 
    else
    {
        var redirectUrl = Url.Action("Index", "Home");
        return Json(new { redirectUrl }, JsonRequestBehavior.AllowGet); // 未登录场景也返回JSON让前端跳转
    }
}

2. 完善前端AJAX逻辑

确保chosenMood变量有有效值,同时增加跳转地址的有效性判断,避免再次出现undefined跳转:

// 先校验参数是否有效
if (!chosenMood) {
    alert('请选择情绪后再操作');
    return;
}

$.ajax({
    url: '@Url.Action("UsersHome", "UsersPage")',
    type: 'POST',
    data: {
            chosenMood: chosenMood    
          },
    success: function(response) {
            console.log('Success:', response);
            // 确认跳转地址存在再执行跳转
            if (response?.redirectUrl) {
                window.location.href = response.redirectUrl;
            } else {
                alert('跳转地址无效,请重试');
            }
    },
    error: function(xhr, status, error) {
            console.error('Error:', error);
            alert('操作失败,请稍后重试。');
    }
});

3. 验证路由配置(可选)

如果修复后仍有问题,检查项目路由配置,确保UsersPage控制器的UsersHome方法能被正确路由。默认MVC路由{controller}/{action}/{id}无需额外配置,若有自定义路由,需保证路由规则能匹配该方法。

内容的提问来源于stack exchange,提问作者Cypher

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 05:42:56