【问题标题】:Why is Jquery sending "undefined=undefined" as my post parameters instead of sending the array data?为什么 Jquery 发送“undefined=undefined”作为我的 post 参数而不是发送数组数据?
【发布时间】:2010-08-13 15:53:24
【问题描述】:

我正在尝试向我的 Web 服务器发送一个 javascript 数组以获取 ajax 请求。这是我的代码:

    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);
            } 
        });
    }

数组的形成是正确的,当我点击$.ajax() 调用时,Chrome 的开发者工具将标签对象显示为具有 2 个元素的数组(所有元素都只是字符串)。

但是,根据 fiddler 的说法,实际发送到服务器的 post 参数是:

undefined=undefined

我做错了什么?

编辑 Console.Log 显示:

console.log(tags)
["portability", "testing"]
undefined

【问题讨论】:

  • tags 不是关联数组,所以我想 jQuery 很难从中构建查询字符串。虽然我很惊讶它会将其视为undefined,但这可能就是问题所在。

标签: jquery ajax arrays


【解决方案1】:

console.log(tags) 对标签有什么看法?

尝试这样发送:

data : ({tags : tags})

【讨论】:

  • 向问题添加了 console.log 输出
  • 这成功了! (在尝试您的解决方案之前我忘了刷新我的页面)
猜你喜欢
  • 1970-01-01
  • 2021-11-09
  • 1970-01-01
  • 2011-06-30
  • 2011-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多