Post array of strings to web API method

前端 未结 7 1294
伪装坚强ぢ
伪装坚强ぢ 2021-02-14 14:42

this is my client side ajax call:

    var list = [\"a\", \"b\", \"c\", \"d\"];

    var jsonText = { data: list };

    $.ajax({
        type: \         


        
7条回答
  •  一整个雨季
    2021-02-14 15:13

    For passing simply types, the data to post must take the form of a name value pair with the name portion being an empty string. So you need to make the Ajax call like so:

    $.ajax({
      type: "POST",
      url: "/api/values",
      data: { "": list },
      dataType: "json",
      success: function() { alert("it worked!"); },
      failure: function() { alert("not working..."); }
    });
    

    Additionally, on your Web API action, annotate it w/ the [FromBody] attribute. Something like:

    public void Post([FromBody]string[] values)
    

    That should do the trick.

提交回复
热议问题