Post array of strings to web API method

前端 未结 7 1293
伪装坚强ぢ
伪装坚强ぢ 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:11

    You can also do this with JSON.stringify, which feels more semantically intuitive than putting things in an object with an empty string key. (I am using Web API 2, not sure if that matters.)

    Client code

    $.ajax({
        type: 'POST', //also works for PUT or DELETE
        url: '/api/UserRole',
        contentType: "application/json",
        data: JSON.stringify(["name1", "name2"])
    })
    

    Server Code

    [HttpPost]
    [Route("api/UserRole")]
    public IHttpActionResult AddUsers(string[] usernames)
    

提交回复
热议问题