【问题标题】:Ajax http 500 error Azure not on localAjax http 500 错误 Azure 不在本地
【发布时间】:2015-09-13 06:33:23
【问题描述】:

运行 ajax 请求时,我收到一条错误消息:

加载资源失败:服务器响应状态为 500 (好的)

问题是似乎没有发生服务器错误。当我在本地计算机上运行应用程序但使用 azure 数据库时,我没有收到错误消息。只有已发布的 azure 应用程序会生成此错误。我已经完成了远程调试,即使浏览器显示此错误,服务器也会继续处理请求,几分钟后它会完成请求。好像真的没有服务器错误一样。

服务器大约需要 10 分钟才能完成请求。我相信这与请求很长的事实有关(因为它适用于较小的数据库)。我知道 azure 对免费应用服务级别的 CPU 时间有限制,但我切换到基本(没有 cpu 时间限制),所以这应该不是问题。该请求非常 sql 密集(大约 20k sql 查询)。

Ajax 调用:

  $.ajax({
  async: true,
  cache: false,
  type: "POST",
  url: FooURL,
  contentType: 'application/json',
  dataType: "json",
  data: JSON.stringify(null),
  success: function (error_message) {
    $("#FooBar").removeClass("loading");
  },
  error: function(jqXHR, textStatus, errorThrown) {
  console.log(textStatus, errorThrown);
  }
});

控制器:

  [Authorize]
  [RequireHttpsAttribute]
  public class FooController : Controller
  {
    private FooBarModel foobarModel = new FooBarModel();

    public ActionResult UpdateFooBarModel()
    {
      foobarModel.UpdateModel();
      return Json("Success");
    }

【问题讨论】:

    标签: ajax azure model-view-controller http-error


    【解决方案1】:

    在 azure 中显然存在空闲超时,描述为 here。默认值设置为 4 分钟,如果您在 VM 上运行应用程序,则最多可以配置为 30 分钟。我通过在数据库中创建一个存储当前请求状态的表来解决它。

    表:

    CREATE TABLE [dbo].[MyTable] (
    [UpdateNo] INT IDENTITY (1, 1) NOT NULL,
    [AllDone]  BIT DEFAULT ((0)) NOT NULL,
    CONSTRAINT [MyTable$PrimaryKey] PRIMARY KEY CLUSTERED ([UpdateNo] ASC)
    

    );

    我没有直接调用该方法,而是创建了一个任务并返回更新状态行的 id。

    public ActionResult UpdateFooBarModel()
    {
      int id = foobarModel.StartUpUpdate(); //Creates the status row 
      Task.Run(() => foobarModel.UpdateModel(id));
      return Json(id);
    }
    
    public ActionResult GetUpdateStatus(int UpdateNo)
    {
      bool status = foobarModel.GetUpdateStatus(UpdateNo);
      return Json(status);
    }
    

    Ajax 调用:

    function check_status(StatusId) {
    $.ajax({
      async: true,
      cache: false,
      type: "POST",
      url: GetUpdateStatusURL + "/?UpdateNo=" + StatusId,
      contentType: 'application/json',
      dataType: "json",
      success: function (StatusDone) {
        if (StatusDone == true) {
          console.log("Update done!");
          $("#FooBar").removeClass("loading");
        } else {
          console.log("Not done!")
          setTimeout(function () {
            check_status(StatusId);
          }, 5000); //Check every 5 seconds
        }
      },
      error: function (jqXHR, textStatus, errorThrown) {
        console.log(textStatus, errorThrown);
      }
    });
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-15
      • 2017-11-22
      • 2021-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多