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.

5
  • 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
    – user3559349
    Commented Oct 9, 2018 at 7:31
  • 1
    type: 'GET' => use type: 'POST' for large arrays. Also try removing contentType: 'application/json' because you want to pass an array. Commented Oct 9, 2018 at 7:31
  • nothing happened what about in the browser developer tools console? any errors/warnings/messages? Commented Oct 9, 2018 at 7:40
  • @JaromandaX See this post: stackoverflow.com/questions/2659952/…. It said 8KB maximum for URL query string. Commented Oct 9, 2018 at 7: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 Commented Oct 9, 2018 at 7:42

2 Answers 2

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)

0

I think you should be replace GET to POST

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.