我有一个类似于
的 ajax 调用this.GetTransactionsInputToday = function () {
var status="complete"
$.ajax({
url: '/Management/function1',
contentType: "application/json; charset=utf-8",
data: status,
type: 'GET',
cache: false,
success: function (result) {
return result;
}
});
}
我也尝试过这样做
this.GetTransactionsInputToday = function () {
var status="complete";
$.ajax({
url: '/Management/function1/' + status,
type: 'GET',
cache: false,
success: function (result) {
return result;
}
});
}
我的管理 Controller 类中有一个 Controller 函数
public JsonResult function1(string status)
{
Some code here..
}
问题是每次调用 function1 时,status 值都会为 null。 任何人都可以告诉我我哪里出了问题吗?
最佳答案
您需要为发送的数据定义名称data: {'status': status}
:
this.GetTransactionsInputToday = function () {
var status="complete"
var r = '';
$.ajax({
url: '/Management/function1',
contentType: "application/json; charset=utf-8",
data: {'status': status},
type: 'GET',
cache: false,
success: function (result) {
r = result;
}
});
return r;
};
此外,您的 this.GetTransactionsInputToday
也不会按您的预期返回结果。 ajax 函数的成功处理程序被异步调用。因此,您的 return r
语句将返回 '',因为它在 Ajax 请求完成之前被调用。
关于jquery - 使用 jquery 的 AJAX 调用将字符串参数发送到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13970313/