首页 > 解决方案 > Datatables ajax 发布请求失败,状态码为 500

问题描述

以下发布请求失败并出现 HTTP 错误 500,但我不明白为什么。

请求如下所示:

var table = table = $('#table-styling').DataTable({
  serverSide: true,
  pageLength: 25,
  processing: true,
  language: {
      loadingRecords: "Loading...",
      processing: "Loading..."
  },
  ajax: {
      url: '/myapplication/myrequest',
      type: 'POST',
      contentType: "application/json;",
      dataType: 'json',
      traditional: true,
      data: function (d) {
          return JSON.stringify(d);
      },
      headers: {
          AntiForgeryToken: $(':input[name="__RequestVerificationToken"]').val()
      }
  },

// etc.  

请求标头如下所示:

Request URL: https://myapplication.com/myrequest
Request Method: POST
Status Code: 500 
Remote Address: 1.1.1.1:443
Referrer Policy: no-referrer-when-downgrade
content-type: text/html; charset=utf-8
date: Fri, 15 May 2020 14:38:22 GMT
server: Kestrel
status: 500
x-powered-by: ASP.NET
:authority: myapplication.com
:method: POST
:path: myapplication/myrequest
:scheme: https
accept: application/json, text/javascript, */*; q=0.01
accept-encoding: gzip, deflate, br
accept-language: en-GB,en-US;q=0.9,en;q=0.8
antiforgerytoken: xxx
content-length: 1600
content-type: application/json;
cookie: xxx
origin: https://myapplication.com
referer: https://myapplication.com/myrequest
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: same-origin
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36
x-requested-with: XMLHttpRequest

这是一个 ASP.Net MVC 应用程序,我的控制器返回视图,它会触发这个 post 请求,如下所示:

@section Scripts {
    <script type="text/javascript">
        $(document).ready(function () {
            myscript.function.load();
        });
    </script>
}

标签: asp.net-mvchttphttp-error

解决方案


正如 HTTP 错误代码所建议的那样,问题是服务器端问题。

所以很自然地我在我的控制器中放置了一个断点,但是在调试时断点没有被命中。

我需要做的是使用 IIS Express 启动我的 Web 应用程序,然后断点才被击中,并且我能够调试真正的服务器端问题。


推荐阅读