user3313278
user3313278

Reputation: 29

Pass Javascript array to controller using JQuery Ajax

I want to pass value of selected input checkbox to controller using Jquery Ajax.

var selected = [];
$('.myCheckBox input:checked').each(function() {
    selected.push($(this).attr('value'));
});

$.ajax({
    url: '@Url.Action("test", "Display")',
    type: 'GET',
    data: {
        MySelectdFile: selected
    },
    dataType: 'json',
    traditional: true,
    contentType: 'application/json',
    success: function(data) {
        alert("OK")
    }
},
error: function(xhr, status, error) {
    var err = eval("(" + xhr.responseText + ")");
    alert(err.Message);
}, complete: function() {
    $('#loading').hide();
}
});
[HttpGet]
public ActionResult test(string[] MySelectdFile) 
{
  foreach(string item in MySelectdFile) 
  {
    //Do Something
  }
}

When my Javascript array length is small ,everything works fine, but when my Javascript array length is big ,nothing happened.

Upvotes: 0

Views: 1409

Answers (2)

Amos Li
Amos Li

Reputation: 1

I think you should be replace GET to POST

Upvotes: 0

ubaldisney
ubaldisney

Reputation: 66

Try change the type from GET to POST from ajax call and from Controller ([HttpGet] to [HttpPost]) Remember:

when sending data, the GET method adds the data to the URL; and the length of a URL is limited (maximum URL length is 2048 characters)

Upvotes: 2

Related Questions