最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pass string array as data in jquery ajax to web api - Stack Overflow

programmeradmin2浏览0评论

I am trying to pass string array to a Web Api method which accepts an array of strings as argument. Bellow my Web Api method

    [HttpGet]
    public string HireRocco(string[] hitList)
    {
        string updateList = string.Empty;
        return updateList;
    }

My ajax

var uri = 'http://localhost:16629/api/AssassinApi/HireRocco',
hitList = ['me', 'yourself'];

$.ajax({
    url: uri,
    type: 'GET',
    data: { hitList : hitList },
    cache: false,
    dataType: 'json',
    async: true,
    contentType: false,
    processData: false,
    success: function (data) {
    },
    error: function (data) {
    }
});

The above ajax successfully hits the HireRocco method but hitList param is still null. What should I change to pass an array of strings as param.

I am trying to pass string array to a Web Api method which accepts an array of strings as argument. Bellow my Web Api method

    [HttpGet]
    public string HireRocco(string[] hitList)
    {
        string updateList = string.Empty;
        return updateList;
    }

My ajax

var uri = 'http://localhost:16629/api/AssassinApi/HireRocco',
hitList = ['me', 'yourself'];

$.ajax({
    url: uri,
    type: 'GET',
    data: { hitList : hitList },
    cache: false,
    dataType: 'json',
    async: true,
    contentType: false,
    processData: false,
    success: function (data) {
    },
    error: function (data) {
    }
});

The above ajax successfully hits the HireRocco method but hitList param is still null. What should I change to pass an array of strings as param.

Share Improve this question edited Mar 6, 2015 at 9:40 hutchonoid 33.3k15 gold badges101 silver badges106 bronze badges asked Mar 6, 2015 at 8:45 rikiriki 2,4237 gold badges42 silver badges75 bronze badges 1
  • @Rakesh_Kumar: its a GET method, anyway if I make a POST it won't resolve the problem – riki Commented Mar 6, 2015 at 8:56
Add a ment  | 

4 Answers 4

Reset to default 4

If you need to send data via a HttpGet, you can add [FromUri] you can edit your controller action as follows and your JavaScript should work as is:

[HttpGet]
public string HireRocco([FromUri] string[] hitList)
{
    string updateList = string.Empty;
    return updateList;
}

Remove contentType: false then set processData to true so it can append the postData your url, as that's how a get request works or you will have to change your api to accept POST request which are set through the header.

$.ajax({
    url: uri,
    type: 'GET',
    data: { hitList : hitList },
    cache: false,
    dataType: 'json',
    async: true,
    processData: true,
    success: function (data) {
        console.log(data);
    },
    error: function (data) {
    }
});

First of all i suggest that you use POST rather than GET. create a javascript array. push the data inside it. send it to web api action method by using JSON.Stringify.. and then process the further logic.

In web api create a model variable.. and create a list object..

Following is the code..

Javascript

var demoarray=[];
demoarray.push({"test1":"hi", "test2":"hello"}); //test1 and test2 are model variable names in web api and hi and hello are their values

you can repeat the process in for loop or something for adding multiple values.

 $.ajax({
      url:"http://localhost..",
      type: "POST",
      data: JSON.Stringify(demoarray),
      contentType: "application/json",
      success: function(data)
               {
               },
      error: function(data)
             {
             }
       });

WEB API Code Create a model class and two properties

public string test1 {get; set;}
public string test2 {get; set;}

controller code

[Httppost]
public void actionmethod(List<modelclass> obj)
{
  int i=0;
  for(i=0; i<obj.count; i++)
  {
    //your logic
  }
}

from the question: How to pass an array as a querystring in a GET request by using JQuery AJAX

answer:

var uri = 'http://localhost:16629/api/AssassinApi/HireRocco',
hitList = ['me', 'yourself'];

$.ajax({
    url: uri,
    type: 'GET',
    data: { hitList : hitList },
    traditional: true,
    success: function (data) {
    },
    error: function (data) {
    }
});

need to set traditional: true

Passed on .Net 8.0

发布评论

评论列表(0)

  1. 暂无评论