繁体   English   中英

Ajax 调用在我的 ASP.NET MVC 4 项目中偶尔返回 403 错误

[英]Ajax calls sporadically returning a 403 error in my ASP.NET MVC 4 project

我刚刚注意到当我将我的 ASP.NET MVC 4 项目发布到我们的 UAT web 服务器上时出现的问题,我在本地测试时没有得到。 我有一个带有下拉列表的表单,其中包含 AJAX 调用以从存储过程中获取值。 这些调用似乎随机返回 403 禁止错误,我无法确定原因。 被调用的方法一会儿工作正常,然后 403。 任何提示将不胜感激。 请参阅以下详细信息:

Ajax JQuery 电话:

    $.fn.GetOriginalValue = function() {
        var cobId = $("#startcob").val();
        var sourceSystemId = $("#SelectedSourceSystemID").val();
        var sourceSystem = $("#SelectedSourceSystemName").val();
        var metricName = $("#SelectedMetricName").val();

        var clientId;
        var dataToSend;

        if (isJuno) {
            clientId = $("#ClientID").val();
            var key2 = $("#key2").val();
            var key3 = $("#key3").val();
            var key4 = $("#key6").val();
            var key5 = $("#key9").val(); 
            var currency = $("#cmdCurrency").val();
            dataToSend = {
                key1: clientId,
                key2: key2,
                CobId: cobId,
                key3: key3,
                key4: key4,
                key5: key5,
                metricName: metricName,
                currency: currency,
                sourceSystem: sourceSystem
            };
        } 

        if (dataToSend != null) {
            $.ajax({
                cache: false,
                type: 'POST',
                url: '@Url.Action("GetCurrentValueJuno")',
                data: dataToSend,
                success: function(data) {
                    if (data.success && data.currentValue != null) {
                        $("#OriginalValue").val(data.currentValue);
                    } else {
                        $("#OriginalValue").val("");
                    }
                }
            });
        }
    };

Controller 方法:

    /// <summary>
    /// Lookup the current value of a metric
    /// </summary>
    /// <param name="key1"></param>
    /// <param name="key2"></param>
    /// <param name="cobId"></param>
    /// <param name="key3"></param>
    /// <param name="key4"></param>
    /// <param name="key5"></param>
    /// <param name="metricName"></param>
    /// <param name="currency"></param>
    /// <param name="sourceSystem"></param>
    /// <returns></returns>
    [AllowCrossSiteJson]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult GetCurrentValueJuno(
        int? key1,
        int? key2,
        DateTime? cobId,
    string key3,
        int? key4,
        int? key5,
        string metricName,
        string currency,
        string sourceSystem
        )
    {
        if (key1 != null && key2 != null && cobId != null)
        {
            //method calls stored procedure to obtain current value based on inputs provided
            var metrics = CFAQueries.GetCurrentValueJuno(
                key1,
                key2,
                cobId,
                key3,
                key4,
                key5,
                metricName,
                sourceSystem);

            var currentValue = metrics?.Value ?? 0;

            if (!string.IsNullOrEmpty(currency))
            {
                var fxrate = GetFxRate((DateTime)cobId, currency);
                currentValue = currentValue / (fxrate ?? 1);
            }

            return Json(
                new
                {
                    currentValue = currentValue,
                    success = metrics != null
                },
                JsonRequestBehavior.AllowGet);
        }

        return Json(
            new
            {
                success = false
            },
            JsonRequestBehavior.AllowGet);
    }

屏幕截图显示带有方法调用的“网络”选项卡,一个失败,一个成功,相隔片刻,具有完全相同的表单输入。

在调查之后,我尝试将以下内容添加到我的 web.config 中:

  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>

而且我还尝试了接受的答案: 在 ASP.Net MVC 中设置访问控制允许来源 - 最简单的方法

但是都没有解决我的问题。 任何帮助或建议将不胜感激。 谢谢你。

在此处输入图像描述

在此处输入图像描述

你有多个访问控制允许来源

你有多个访问控制允许来源

使用多个域检查此问题 Access-control-allow-origin

在这个问题上困扰了两天之后......

我的项目在我的开发机器和我客户的服务器上运行良好,但只能直接在该服务器上运行。 从“the inte.net”运行查询时出现错误

客户端安装了一个阻止请求并返回 403 错误的 WAF。 我在 IIS 中找不到关于这些错误的任何日志,它应该更快地提示我

我不知道 Barrassment 的情况是否相同,但我正在分享我的解决方案,也许它可以帮助其他人......

工作了三年的代码......在多台服务器上

$.ajax({
    type: "POST",
    url: "/People/Typeahead",
    data: "{'query':'" + query + "'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {}
})

现在有效的代码,WAF 无法用单引号作为分隔符解析 JSON...所以我的解决方案只是使用 JSON.stringify:

$.ajax({
    type: "POST",
    url: "/People/Typeahead",
    data: JSON.stringify({
        query: query
    }),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {}
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM