jQuery $.ajax 将 null 值传递给 MVC 控制器
我已经为这个问题苦苦挣扎了一段时间了。我已遵循类似问题中提供的所有建议,但没有成功。
正如标题所示,我的控制器未接收到我的 $.ajax
传递的数据。
jQuery:
var data = {
id: id,
app: app
};
$.ajax({
type: "POST",
url: "/Utility/FetchTransactionLog",
data: JSON.stringify(data),
contentType: "application/json;",
success: function (data) {
if (data) {
h.resultDiv.html(data);
}
else {
h.resultDiv.html("");
alert("No Log Found");
}
}
});
控制器:
//id and app receives null values
public ActionResult FetchTransactionLog(string id,string app) {
UtilityModels util = new UtilityModels(app);
List<ResultModel> result = util.FetchTransactionLog(id);
return View("LogResult", result);
}
Global.asax 中的路由:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Utility", action = "Index", id=UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"FetchLog",
"Utility/FetchTransactionLog/{id}/{app}",
new { controller = "Utility", action = "FetchTransactionLog", id = "", app = "" } // Parameter defaults
);
我做错了什么?
更新
以下内容按照 Samich 的建议进行:
$.ajax({
type: "POST",
url: "/Utility/FetchTransactionLog/" + id + "/" + app,
data: "{}",
contentType: "application/json;",
success: function (data) {
if (data) {
h.resultDiv.html(data);
}
else {
h.resultDiv.html("");
alert("No Log Found");
}
}
});
i've been struggling with this problem for quite some time. I've followed all suggestions provided in similar problems but without success.
As the title says, data passed by my $.ajax
is not received by my controller.
jQuery:
var data = {
id: id,
app: app
};
$.ajax({
type: "POST",
url: "/Utility/FetchTransactionLog",
data: JSON.stringify(data),
contentType: "application/json;",
success: function (data) {
if (data) {
h.resultDiv.html(data);
}
else {
h.resultDiv.html("");
alert("No Log Found");
}
}
});
Controller:
//id and app receives null values
public ActionResult FetchTransactionLog(string id,string app) {
UtilityModels util = new UtilityModels(app);
List<ResultModel> result = util.FetchTransactionLog(id);
return View("LogResult", result);
}
Route in Global.asax:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Utility", action = "Index", id=UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"FetchLog",
"Utility/FetchTransactionLog/{id}/{app}",
new { controller = "Utility", action = "FetchTransactionLog", id = "", app = "" } // Parameter defaults
);
What am i doing wrong?
Update
The following works as suggested by Samich:
$.ajax({
type: "POST",
url: "/Utility/FetchTransactionLog/" + id + "/" + app,
data: "{}",
contentType: "application/json;",
success: function (data) {
if (data) {
h.resultDiv.html(data);
}
else {
h.resultDiv.html("");
alert("No Log Found");
}
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试将您的 id 和应用程序附加到您在路由
/Utility/FetchTransactionLog/{id}/{app}
中指定的网址,可能是因为您的路由需要查询字符串中的值,但找不到它在那里,所以路由注册提供的默认值
try just to append your id and app to your url as you specified in the routes
/Utility/FetchTransactionLog/{id}/{app}
Probably because your routing expects values in query string and doesn't find it there, so the default values provided from the routing registration
为什么要对数据进行 JSON.stringify?您应该按原样传递对象,就像
文档 说的那样,如果您通过在对象中,它会自动转换为字符串。通过传入包含 JSON 的字符串,您会混淆 jQuery,并且最终会将垃圾数据传递给 ASP(使用浏览器的开发工具来检查请求会立即产生这很明显)。
Why do you do JSON.stringify your data? You should just pass the object as it is, like
The documentation says flat out that if you pass in an object, it will be converted to a string automatically. By passing in a string that contains JSON, you are confusing jQuery and it ends up passing garbage data to ASP (using your browser's developer tools to inspect the request would have immediately made this apparent).
您的
data
不应该是 JSON 字符串,只需将其保留为 Javascript 对象即可。 jQuery 会将其转换为 POST 参数,然后您的控制器可以读取这些参数。Your
data
shouldn't be a JSON string, just leave it as a Javascript object. jQuery will convert this to POST parameters, which your controller can then read.我目前无法对此进行测试,但尝试将 - 更改
为
JSON.stringify
将以阻止 jQuery 正确发布数据的方式格式化您的数据。如果您传递一个普通对象,一切都应该按预期工作。I can't test this at the moment but try changing -
to
JSON.stringify
will format you data in a way that prevents jQuery posting it correctly. If you pass a normal object everything should work as expected.试试这个:
Try this :