views:

35

answers:

1

I am attempting to send a javascript array to my web server for an ajax request. Here is my code:

    function SearchTasksByTags() {
        // Get the list of tags currently chosen
        var tags = [];
        $('.tagit-choice input').each(function () { tags.push($(this).val()); });

        // If we have no tags, don't bother searching and just clear the current results
        if (tags.length == 0) {
            $('#tagSearchResults').empty();
            return;
        }

        // Retrieve the search results from the server
        $.ajax({ url: '<%= Url.Action("SearchByTags") %>',
            data: tags,
            type: 'POST',
            success: function (html) {
                $("#tagSearchResults").empty().append(html);
            } 
        });
    }

The array is being formed correctly, as when I hit the $.ajax() call Chrome's developer tools show the tags object as being an array with 2 elements (all elements are just strings).

However, according to fiddler, the actual post parameters getting sent to the server are:

undefined=undefined

What am I doing wrong?

Edit Console.Log shows:

console.log(tags)
["portability", "testing"]
undefined
+1  A: 

What does a console.log(tags) say about the tags?

Try sending it like this:

data : ({tags : tags})
Rakward
Added console.log output to question
KallDrexx
This worked! (I forgot to refresh my page before attempting your solution)
KallDrexx