使用 AJAX 调用 jquery 将字符串参数发送到控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13970313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send string parameter to controller using AJAX call for jquery
提问by Gautam
I have a ajax call that goes like
我有一个类似的 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;
}
});
}
I have also tried doing like this
我也试过这样做
this.GetTransactionsInputToday = function () {
var status="complete";
$.ajax({
url: '/Management/function1/' + status,
type: 'GET',
cache: false,
success: function (result) {
return result;
}
});
}
I have a controller function in my management controller class
我的管理控制器类中有一个控制器功能
public JsonResult function1(string status)
{
Some code here..
}
The issue is that every time function1 is called teh value of status comes as null. CAn any one please let me know where am I going wrong??
问题是每次调用 function1 时,状态值都为空。任何人都可以让我知道我哪里出错了??
回答by Viral Patel
You need to define name for the data you sending data: {'status': status}
:
您需要为发送的数据定义名称 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;
};
Also your this.GetTransactionsInputToday
will not return result as you expected. The success handler of ajax function gets called asynchronously. So your return r
statement will return ''as it gets called before the Ajax request gets completed.
你this.GetTransactionsInputToday
也不会像你预期的那样返回结果。ajax 函数的成功处理程序被异步调用。因此,您的return r
语句将在 Ajax 请求完成之前调用它时返回''。
回答by anmarti
You might construct the json parameter wrongly.
您可能错误地构造了 json 参数。
using json:
data: "{'prop1': '" + value1+ "'}"
使用json:
data: "{'prop1': '" + value1+ "'}"
$.ajax({
url: '/Management/function1',
contentType: "application/json; charset=utf-8",
data: "{'prop1': '" + value1+ "'}",
type: 'GET',
cache: false,
success: function (result) {
return result;
}
});
回答by PhearOfRayne
Try setting the variable outside the ajax function and then return it, this way its in the right scope. Also in order to get an immediate value you need to make this ajax call synchronous.
尝试在 ajax 函数之外设置变量,然后返回它,这样它就在正确的范围内。此外,为了获得即时值,您需要使此 ajax 调用同步。
this.GetTransactionsInputToday = function () {
var status = "complete";
var r = '';
$.ajax({
async : false,
url: '/Management/function1',
contentType: "application/json; charset=utf-8",
data: {'status' : status},
type: 'GET',
cache: false,
success: function (result) {
r = result;
}
});
return r;
};