ajax PUT does not transfer array

问题: I use javascript in my webpage to send a GUID (url parameter) + a GUID array to the webservice. But when I try to send the GUID array, the parameter stays everytime empty....

问题:

I use javascript in my webpage to send a GUID (url parameter) + a GUID array to the webservice. But when I try to send the GUID array, the parameter stays everytime empty.

my Javascript looks like this:

//Save Button is clicked
function saveBtnClicked() {
    var currentDataSetGuid = $("#currentDataSetGuid").val();
    var Table = $("#Table").find("tbody").first();
    var selectedElements = Table.find("input:checked");
    var saveGuidArray = new Array();
    var i = 0;
    while (i < selectedElements.length) {
        var trid = selectedElements[i].parentElement.parentElement.id;
        saveGuidArray.push(trid);
        i = i + 1;
    }

    putSave(currentDataSetGuid,saveGuidArray)
}

//Save action to call the controller
function putSave(currentDataSetGuid, saveGuidArray) {
    $.ajax({
        dataType: "json",
        cache: false,
        method: "PUT",
        url: "/api/myAPP/SaveEndpoint/" + currentDataSetGuid,
        contentType: "application/json",
        data: JSON.stringify({ paramName: saveGuidArray}),
        success: function (result) {
            showSuccess("Save was successfull");
        },
        error: function (error) {
            showError("Error while saving");
        }
    });
}

I also tried - but with the same result:

data: JSON.stringify(saveGuidArray),

And here is my implementation of the Controller Interface (C#)

    [HttpPut]
    [Route("/api/myAPP/SaveEndpoint/{currentDataSetGuid}")]
    public IActionResult SaveAction(Guid currentDataSetGuid, List<Guid> saveGuidArray)

In my controller, currentDataSetGuid is set every time correctly. But saveGuidArray is empty / has no elements.


回答1:

I resolved the error now. The problem was a plain datatype as HTTP Body.

I added a new class and implemented the controller accordingly.

[HttpPut]
[Route("/api/myAPP/SaveEndpoint/{currentDataSetGuid}")]
public IActionResult SaveAction(Guid currentDataSetGuid, [FromBody] ViewSaveGuid saveGuidArray)

And then I setup the object in the javascript to pass it as JSON.Stringify

var GuidList{
   array: saveGuidArray
};
[...]
data: JSON.stringify(GuidList);

回答2:

Change the Controller to this

[HttpPut]
[Route("/api/myAPP/SaveEndpoint/{currentDataSetGuid}")]
public IActionResult SaveAction([FromUri]Guid currentDataSetGuid, [FromBody]List<Guid> saveGuidArray)

回答3:

Given you have your action defined like this:

[HttpPut]
[Route("/api/myAPP/SaveEndpoint/{currentDataSetGuid}")]
public IActionResult SaveAction(Guid currentDataSetGuid, [FromBody]List<Guid> saveGuidArray)

Then your Ajax request should be formatted this way:

function putSave(currentDataSetGuid, saveGuidArray) {
  if (!currentDataSetGuid || !saveGuidArray) {
    console.log('Error');
    // Handle validation
    return;
  }

  $.ajax({
      dataType: "json",
      cache: false,
      method: "PUT",
      url: "/api/myAPP/SaveEndpoint/" + currentDataSetGuid,
      contentType: "application/json",
      data: JSON.stringify({ saveGuidArray }), // Take note of the difference on this line.
      success: function (result) {
          showSuccess("Save was successfull");
      },
      error: function (error) {
          showError("Error while saving");
      }
  });
}
  • 发表于 2019-03-26 08:11
  • 阅读 ( 164 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除