jquery 在我的ASP.NETMVC4项目中, AJAX 调用偶尔会返回一个403错误

m4pnthwp  于 2023-01-30  发布在  jQuery
关注(0)|答案(2)|浏览(162)

我刚刚注意到一个问题,当我发布我的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("");
                    }
                }
            });
        }
    };

控制器方法:

/// <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);
    }

屏幕截图显示了带有方法调用的Network选项卡,一个失败,一个成功,间隔很短,表单输入完全相同。
经过调查,我尝试添加以下到我的web.config:

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

我也试过这个公认的答案:Setting Access-Control-Allow-Origin in ASP.Net MVC - simplest possible method
不过,这两个都没有解决我的问题。任何帮助或建议将不胜感激。谢谢。

k3bvogb1

k3bvogb12#

在这个问题上纠结了两天之后...
我的项目在我的开发机器和我客户的服务器上运行良好,但只是直接在那个服务器上运行。
客户端安装了一个WAF,它会阻塞请求并返回403错误。我在IIS中找不到任何关于这些错误的日志,它应该更快地向我报告
我不知道这是否是相同的情况尴尬,但我分享我的解决方案,也许它可以帮助别人...
在多台服务器上运行了三年的代码

$.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) {}
})

相关问题