Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Jquery sending "undefined=undefined" as my post parameters instead of sending the array data?

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
like image 539
KallDrexx Avatar asked Aug 13 '10 15:08

KallDrexx


1 Answers

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

Try sending it like this:

data : ({tags : tags})
like image 152
Rakward Avatar answered Oct 19 '22 23:10

Rakward