【问题标题】:In ASP.NET the ajax GET response is null when the HttpStateCode is 200在 ASP.NET 中,当 HttpStateCode 为 200 时,ajax GET 响应为空
【发布时间】:2019-04-22 20:37:04
【问题描述】:

我尝试在 ASP.NET 项目中使用 ajax GET 请求获取 Logged User 信息。 在浏览器中,响应代码为 200,但我得到“空”响应。 这是我尝试过的一些代码。

    function display(json) {
        console.log(json);
    }
    var config = {
        "async": true,
        "crossDomain": true,
        "url": ApiBaseUrl + "GetUserDetail",
        "method": "GET",
        "headers": headers,
        "contenttype": "application/json; charset=utf-8",
        success: display,
    };

    $.ajax(config).done(function (response) {
        if (response) {
            console.log(response);
        }

        console.log("No Data");
    });

后端控制器代码为:

        [Route("GetUserDetail")]
        [HttpGet()]
        public IHttpActionResult Get()
        {
            try
            {
                var identity = (ClaimsPrincipal)Thread.CurrentPrincipal;
                string email = identity.Claims.Where(c => c.Type == ClaimTypes.Email)
                               .Select(c => c.Value).SingleOrDefault();
                IBOAccount _accountBO = IOContainer.Get<IBOAccount>();
                var _registeredUser = "response";
                return Ok(_registeredUser);
            }
            catch (Exception)
            {
                throw;
            }
        }

怎么了? cookie中好像没有保存token信息。

【问题讨论】:

  • 你是否只尝试了两行代码 var _registeredUser = "response";返回确定(_registeredUser);可能是您的代码中的异常

标签: jquery asp.net asp.net-mvc


【解决方案1】:

如果您使用的是 ASP.NET MVC 5.2(除 ASP.NET Core 1.X、2.X 以外的任何版本),则需要明确允许您的 get 请求通过 JsonRequestBehavior 属性返回 JSON。因此,您的退货声明可能类似于:

return Json(_registeredUser, JsonRequestBehavior.AllowGet);

如果您使用的是 ASP.NET Core,那么我可能会猜测您的控制器中发生了一些异常。

【讨论】:

    【解决方案2】:

    如果没有Token授权,则无法工作,响应为NULL。 通过在请求标头中使用“授权”,我得到了登录用户的电子邮件地址和名称。

    这里有一些使用Token的代码。

        var AuthData = JSON.parse(UserCustomService.getSessionStorage("Token")); //get Token
        var headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Accept": "application/x-www-form-urlencoded",
            "cache-control": "no-cache",
            "Authorization": "Bearer " + AuthData.access_token, // Bearer:type of Token
        };
    
        var GetUserInformation = function () {
    
            var config = {
                "async": true,
                "crossDomain": true,
                "url": ApiBaseUrl + "/GetUserInformation", // user defined route
                "method": "GET",
                "headers": headers
            };
    
            $.ajax(config).done(function (response) {
                if (response) {
                    console.log(response);
                } else console.log("NULL response");
            });
        }
    

    出于安全考虑,我认为令牌应该包含在所有请求标头中,用于获取和更新数据库中的当前用户信息。

    【讨论】:

      【解决方案3】:

      试试这个代码:

      var AuthData = JSON.parse(UserCustomService.getSessionStorage("Token")); 
      var headers = {
          "Content-Type": "application/x-www-form-urlencoded",
          "Accept": "application/x-www-form-urlencoded",
          "cache-control": "no-cache",
          "Authorization": "Bearer " + AuthData.access_token, 
      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多