$.ajax POST/non post 行为差异?
我正在尝试通过 JSON 将一些数据发送到 MVC 控制器操作:
出于某种原因,这可以工作:
var items = [];
$("input:checked").each(function () { items.push($(this).val()); });
//This works
$.ajax({
type: "POST",
url: url,
data: { listofIDs: items, personID: personID},
dataType: "json",
traditional: true,
success: function() {
//Rebind grid
}
});
//This listofIDs is ALWAYS null !? (longhand for `$.getJSON` ?)
$.ajax({
url: url,
dataType: 'json',
data: { listofIDs: items, personID: personID },
success: function () {
//Rebind grid
}
});
那么为什么它在顶部工作,但底部总是为空?相同的代码用于构建 items
!?
编辑:控制器方法
public ActionResult AddJson(List<int> listofIDs, int personID)
{
if (listofIDs==null || listofIDs.Count < 1)
return Json(false, JsonRequestBehavior.AllowGet);
...
//Add something to database
//Return true if suceeed, false if not
}
编辑: 所以我最终通过将数组转换为字符串并以这种方式发送来解决它。这样我就能够发送的不仅仅是数组变量。
var items = $(':input:checked').map(function () { return $(this).val();}).toArray();
var stringArray = String(items);
$.ajax({
url: url,
dataType: 'json',
data: { listOfIDs: stringArray, personID: personID },
success: function () {
//rebind grid
}
});
请注意,无需设置 POST
类型。
I am attempting to send some data via JSON to a MVC controller action:
For some reason, this works:
var items = [];
$("input:checked").each(function () { items.push($(this).val()); });
//This works
$.ajax({
type: "POST",
url: url,
data: { listofIDs: items, personID: personID},
dataType: "json",
traditional: true,
success: function() {
//Rebind grid
}
});
//This listofIDs is ALWAYS null !? (longhand for `$.getJSON` ?)
$.ajax({
url: url,
dataType: 'json',
data: { listofIDs: items, personID: personID },
success: function () {
//Rebind grid
}
});
So why does it work at the top, but the bottom it is always null? The same code is used to build items
!?
edit: controller method
public ActionResult AddJson(List<int> listofIDs, int personID)
{
if (listofIDs==null || listofIDs.Count < 1)
return Json(false, JsonRequestBehavior.AllowGet);
...
//Add something to database
//Return true if suceeed, false if not
}
edit: so I ended up solving it by just turning the array into a string and sending it that way. That way I was able to send more than just the array variable.
var items = $(':input:checked').map(function () { return $(this).val();}).toArray();
var stringArray = String(items);
$.ajax({
url: url,
dataType: 'json',
data: { listOfIDs: stringArray, personID: personID },
success: function () {
//rebind grid
}
});
Note no POST
type needed to be set.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果没有
type: "POST"
,则默认为GET
(根据文档),您的服务器端代码可能不期望这样。另外,您可以使用... jsFiddle 获取值列表
。
但不确定它是否更好。只是我的一个想法:)
Without
type: "POST"
it defaults toGET
(according to the docs), which your server side code is probably not expecting.Also, you could get a list of the values with...
jsFiddle.
Not sure if it's better though. Just an idea I had :)
问题可能出在服务器端。在第一种情况下,您在另一个 HTTP GET 中使用 HTTP POST。这意味着您可能必须以不同的方式访问数据。也许看看 HTTP GET 案例的 从 Uri 获取单独的查询参数 。
The problem is probably on the server side. In the first case you are using HTTP POST in the other HTTP GET. That means that you probably have to access the data differently. Maybe have a look at Get individual query parameters from Uri for the HTTP GET case.
您没有指定请求的类型,因此默认为
GET
。来自 jQuery 文档:
也许您想指定
POST
。如果您使用 GET 发送数组,该数组将像?listofIDs=...
一样添加到QUERY_STRING
中,并且无法像通常访问 POST 数据一样进行访问。You're not specifying the type of the request, so it defaults to
GET
.From jQuery Docs:
Perhaps you meant to specify
POST
. If you send it using GET the array will be added into theQUERY_STRING
like?listofIDs=...
and won't be accessible the same way you normally access POSTed data.