【问题标题】:In Asp.net, 'Post' Ajax Request Will 'Get'在 Asp.net 中,“发布”Ajax 请求将“获取”
【发布时间】:2021-08-15 12:07:12
【问题描述】:

在我的 Asp.net 项目和名为“AjaxController”的控制器中,我有这个操作方法:

[HttpPost]
public ActionResult GetList(int year)
{
  var res="";
  // some codes 
  return Json(res);
}

在 Js 文件中:

$.ajax({
    url: '/Ajax/GetList/',
    type: "POST",
    contentType: "application/x-www-form-urlencoded; charset=UTF-8",
    data: 2000,
    async: false,
    success: function (response) {
    // some codes  
    },
    error: function (xhr, status, error) {
        alert(error);
    },
});

我希望这个方法只能用 'POST' 调用,但是当我检查我的日志时,我会看到一些错误,例如:

AbsoluteUri :https://example.com/Ajax/GetList/
* Message :A public action method 'GetList' was not found on controller 'Controllers.AjaxController'.

这显示称为“GET”,而不是“POST”。 什么和哪里有问题? 进阶致谢

【问题讨论】:

  • 请回答。 stackoverflow 中没有一个对我有用的问题的答案

标签: asp.net ajax post get


【解决方案1】:

我的一些建议-

  • 删除 contentType 和 async 属性,除非确实需要
  • 将 JSON 对象传递给“数据”
  • 另外,大量使用调试器和断点来找出自己的代码误入歧途的地方
$.ajax({
    url: '/Ajax/GetList/',
    type: "POST",
    //contentType: "application/x-www-form-urlencoded; charset=UTF-8",
    data: {year: 2000},
    //async: false,
    success: function (response) {
    debugger;
    // some codes  
    },
    error: function (xhr, status, error) {
        debugger;
        alert(error);
    },
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多