Vahid Akbari
Vahid Akbari

Reputation: 189

send list as a parameter with ajax

i want to send a list as parameter with ajax but in my function parameter is null. i can solve this problem with json but IE 7 do not support json.this is my code. how can i solve this????

 $(function () {
     $('#DeleteUser').click(function () {
         var list = [];
         $('#userForm input:checked').each(function () {
             list.push(this.id);
         });
         $.ajax({
             url: '@Url.Action("DeleteUser", "UserManagement")',
             data: {
                 userId: list
             },
             type: 'POST',
             success: function (result) {
                 alert("success");
             },
             error: function (result) {
                 alert("error!");
             }
         }); //end ajax
     });
 });

Upvotes: 1

Views: 3857

Answers (3)

Vahid Akbari
Vahid Akbari

Reputation: 189

i find the answer.check this site http://bestiejs.github.io/json3/

Upvotes: 1

Rune FS
Rune FS

Reputation: 21742

There might be a few reasons why this would fail

From the code it would seem you a using razor. If this is not in a razor view (Ie a .cshtml file) your link will be incorrect.

If the link is correct then the code would work assuming that your controller is setup correctly. Since the data will not be part of the URL you need to specify FromBody for the userId parameter.

public ActionResult DeleteUser([FromBody] userId){
    //implementation
}

alternatively then you could append the list to the URL

url: '@Url.Action("DeleteUser", "UserManagement")/' + list 

and of course have a route in global.asax that sends the first argument to userId

Upvotes: 0

emran
emran

Reputation: 922

Add Douglas Crockford's famous json2.js, and use feature detection to polyfill json serialization for compatibility

https://github.com/douglascrockford/JSON-js

from documentation:

JSON.stringify(value, replacer, space)
value       any JavaScript value, usually an object or array.

replacer    an optional parameter that determines how object
            values are stringified for objects. It can be a
            function or an array of strings.

space       an optional parameter that specifies the indentation
            of nested structures. If it is omitted, the text will
            be packed without extra whitespace. If it is a number,
            it will specify the number of spaces to indent at each
            level. If it is a string (such as '\t' or ' '),
            it contains the characters used to indent at each level.

proposed code change:

{ ... data: JSON.stringify(list) ... }

Upvotes: 0

Related Questions