首页 > 解决方案 > 后端没有错误,但 Ajax 功能失败

问题描述

我有一个 Ajax 调用和一个带有一些方法的控制器,但有些奇怪。

Ajax 调用可以到达Controller Method。该方法完美地返回值,没有错误,什么都没有。但是,在 AJAX 中,返回带有错误(不是 Ajax 成功)。

此外,如果我检查返回值,我可以在 responseJson 上看到正确的值,但状态为 404,statusText 为“error”({“readyState”:4,“status”:404,“statusText”:“error”})

有人知道发生了什么吗?后端没有错误,但是 Ajax 有错误。

控制器方法

public JsonResult SceneListUpdated(string sceneId)
        {
            SceneListModel model = new SceneListModel();
            var licenseValidationState = KeygenLicenseState.CheckLicense();
            if (licenseValidationState == 0)
            {
                SceneListService sceneService = new SceneListService();
                model = sceneService.GetSceneListUpdated(sceneId);
                return Json(new { Success = true, data = model }, JsonRequestBehavior.AllowGet );
            }
            return Json(new { Success = false, data = "" }, JsonRequestBehavior.AllowGet);
        }

阿贾克斯函数

 $.ajax({
        url: "/api/test/SceneList/SceneListUpdated?sceneId=" + sceneIdAux,  //Method in controller
        type: "GET",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        success: function (e) {
            
        },
        error: function (data) {
        }
    });

在此处输入图像描述

标签: c#asp.netajaxasp.net-mvc.net-core

解决方案


尝试修复ajax

 $.ajax({
        url: "/api/test/SceneList/SceneListUpdated/"+ sceneIdAux,  //Method in controller
        type: "GET",
         success: function (e) {
          
        },
        error: function (data) {
        }
    });

也许您可以尝试其中一种路由,因为您有 404

[Route("{sceneId}")]
[Route("~/api/SceneList/SceneListUpdated/{sceneId}")]
public JsonResult SceneListUpdated(string sceneId)

推荐阅读