【发布时间】: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,但这可能就是问题所在。