微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Asp.net 中,“发布”Ajax 请求将“获取”

如何解决在 Asp.net 中,“发布”Ajax 请求将“获取”

在我的 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”。 问题出在哪里? 感谢高级

解决方法

我的一些建议 -

  • 除非确实需要,否则删除 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);
    },});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。