在 javascript 中我有以下内容:
$.ajax({
url: "/ajax/test",
type: "POST",
dataType: "html",
data: '{"keyword" : "' + $('#tbxBrand').val() + '", "projectguid" : "<%= thisProject.ProjectGuid.ToString() %>", "userguid" : "<%= thisUser.UserGuid.ToString() %>"}',
beforeSend: function() { },
success: function(data) {
alert(data);
}
});
在 Controller 中我有:
public ActionResult Test()
{
string keyword = Request.Form["keyword"];
return new JsonResult { Data = keyword };
}
但是,Request.Form 不包含正确的键。事实上,Request.Form 的结果似乎不正确:
Request.Form = {%7b%22keyword%22+%3a+%22data%22%2c+%22projectguid%22+%3a+%22cedce659-fd91-46c8-8f69-e527a38cffc2%22%2c+%22userguid%22+%3a+%2252ff20ab-cdf1-4dae-b539-645b6bf461a7%22%7d}
我不明白这里出了什么问题。有人可以帮忙吗?
谢谢!
最佳答案
不要引用数据。对象将被转换为查询字符串。如果您使用字符串,则它需要采用查询字符串格式。另外,我认为您会发现在标签周围使用单引号会更好。如果需要,这将允许您在标签内使用双引号。
$.ajax({
url: "/ajax/test",
type: "POST",
dataType: "html",
data: {
"keyword" : $('#tbxBrand').val(),
"projectguid" : '<%= thisProject.ProjectGuid.ToString() %>',
"userguid" : '<%= thisUser.UserGuid.ToString() %>'
},
beforeSend: function() { },
success: function(data) {
alert(data);
}
});
关于ASP.NET MVC $.ajax POST 未正确提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1210441/