简体   繁体   English

使用jQuery AJAX的POST调用不起作用

[英]POST call using jquery AJAX not working

I am trying to build an ASP.NET MVC web service in which I am trying to make a POST call in javascript using jQuery ajax as below. 我正在尝试构建一个ASP.NET MVC Web服务,其中我试图使用jQuery ajax在javascript中进行POST调用,如下所示。

$.ajax({
      url: "/bug/CreateBug",
      type: "POST",
      data: rowData,
      contentType: "application/json",
      datatype: "json", //return type
      success: function (data) {
          console.log(data);
      },
      error: function (xhr) {
          alert('error');
      }
  });

I keep getting the error TypeError: e is undefined . 我不断收到错误TypeError: e is undefined I tried adding a log statement just before this ajax call and things are working fine. 我尝试在此ajax调用之前添加一条日志语句,并且一切正常。 Not sure what am I missing out on. 不知道我错过了什么。 My rowData looks something like below. 我的rowData如下所示。

{
    "Date": "2016-12-31",
    "Id": "1234-csj4-sadf-random",
    "Scenario": "abc",
    "IsFixed": "No"
}

My C# code in the controller looks something like this 我在控制器中的C#代码看起来像这样

[HttpPost]
public JsonResult CreateBug(string jsonRequest)
{
    var bugId = GetBugId(jsonRequest);

    return this.Json(bugId, JsonRequestBehavior.AllowGet);
}

I tried to test the above POST call using Postman and I got jsonRequest as null . 我尝试使用Postman测试上述POST调用,并且将jsonRequestnull Could someone pls help me out here on how can I get the POST request to work? 有人可以在这里帮助我如何使POST请求生效吗?

Thanks in advance! 提前致谢!

  try it hope it works
  $.ajax({
  url: "/bug/CreateBug",
  type: "POST",
  data: JSON.stringify(rowdata),
  contentType: "application/json",
  datatype: "json", //return type
  success: function (data) {
      console.log(data);
  },
  error: function (xhr) {
      alert('error');
  }
  });

------ on controller do something like this or the best approach is to create model with all these property and MVC will bind it for you. ------在控制器上做类似的事情,或者最好的方法是创建具有所有这些属性的模型,MVC将为您绑定模型。

    [HttpPost]
   public JsonResult CreateBug(string Id, string Date, string IsFixed ,   string Scenario)
 {
    var bugId = GetBugId(jsonRequest);

   return this.Json(bugId, JsonRequestBehavior.AllowGet);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM