簡體   English   中英

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

[英]POST call using jquery AJAX not working

我正在嘗試構建一個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');
      }
  });

我不斷收到錯誤TypeError: e is undefined 我嘗試在此ajax調用之前添加一條日志語句,並且一切正常。 不知道我錯過了什么。 我的rowData如下所示。

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

我在控制器中的C#代碼看起來像這樣

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

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

我嘗試使用Postman測試上述POST調用,並且將jsonRequestnull 有人可以在這里幫助我如何使POST請求生效嗎?

提前致謝!

  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');
  }
  });

------在控制器上做類似的事情,或者最好的方法是創建具有所有這些屬性的模型,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