0

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.

executable
  • 3,365
  • 6
  • 24
  • 52
user3313278
  • 29
  • 1
  • 4
  • Either delete the `contentType: 'application/json',` **OR** delete `traditional: true,` and change to `data: JSON.stringify({ MySelectdFile: selected }),` ad it need to be a POST, not a GET –  Oct 09 '18 at 07:31
  • 1
    `type: 'GET'` => use `type: 'POST'` for large arrays. Also try removing `contentType: 'application/json'` because you want to pass an array. – Tetsuya Yamamoto Oct 09 '18 at 07:31
  • `nothing happened` what about in the browser **developer** tools console? any errors/warnings/messages? – Jaromanda X Oct 09 '18 at 07:40
  • @JaromandaX See this post: https://stackoverflow.com/questions/2659952/maximum-length-of-http-get-request. It said 8KB maximum for URL query string. – Tetsuya Yamamoto Oct 09 '18 at 07:41
  • @TetsuyaYamamoto - doesn't say anything about the limit on the server side - which, according to the answer below is 2KB - also, assuming 8KB is bad - since the same answer states that safari limit is 2KB - when dealing with multiple browsers, you take into consideration what they can ALL do, not the outliers :p – Jaromanda X Oct 09 '18 at 07:42

2 Answers2

2

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)

ubaldisney
  • 66
  • 1
  • 10
0

I think you should be replace GET to POST