jQuery 如何将多个参数从 ajax 调用传递给 MVC 控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20193888/
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
How to pass Multiple Parameters from ajax call to MVC Controller
提问by DS kumar
I have the controller like the below:
我有如下控制器:
public ActionResult Save(string input, string name) {
//Some code
return PartialView();
}
And I need an ajax call to this controller method and pass the two arguments input and value
我需要对这个控制器方法进行 ajax 调用并传递两个参数 input 和 value
And my ajax call is like the below:
我的 ajax 调用如下所示:
$.ajax({
url: '/Home/Save',
type: 'POST',
async: false,
dataType: 'text',
processData: false,
data: "input=" + JSON.stringify(data) + "&name =" + $("#name").val(),
success: function (data) {
}
});
I am unable to pass the value to the name parameter.. the value in the name parameter is becoming null .. please help me .. Thanks in advance
我无法将值传递给 name 参数.. name 参数中的值变为空.. 请帮助我.. 提前致谢
回答by xdumaine
You're making an HTTP POST, but trying to pass parameters with the GET query string syntax. In a POST, the data are passed as named parameters and do not use the param=value&foo=bar
syntax. Using jQuery's ajax method lets you create a javascript object with the named parameters, like so:
您正在执行 HTTP POST,但尝试使用 GET 查询字符串语法传递参数。在 POST 中,数据作为命名参数传递,不使用param=value&foo=bar
语法。使用 jQuery 的 ajax 方法,您可以使用命名参数创建一个 javascript 对象,如下所示:
$.ajax({
url: '/Home/SaveChart',
type: 'POST',
async: false,
dataType: 'text',
processData: false,
data: {
input: JSON.stringify(IVRInstant.data),
name: $("#wrkname").val()
},
success: function (data) { }
});
回答by Himalaya Garg
In addition to posts by @xdumain, I prefer creating data object before ajax call so you can debug it.
除了@xdumain 的帖子之外,我更喜欢在 ajax 调用之前创建数据对象,以便您可以调试它。
var dataObject = JSON.stringify({
'input': $('#myInput').val(),
'name': $('#myName').val(),
});
Now use it in ajax call
现在在ajax调用中使用它
$.ajax({
url: "/Home/SaveChart",
type: 'POST',
async: false,
dataType: 'json',
contentType: 'application/json',
data: dataObject,
success: function (data) { },
error: function (xhr) { } )};
回答by Purnendu Sarkar
function final_submit1() {
var city = $("#city").val();
var airport = $("#airport").val();
var vehicle = $("#vehicle").val();
if(city && airport){
$.ajax({
type:"POST",
cache:false,
data:{"city": city,"airport": airport},
url:'http://airportLimo/ajax-car-list',
success: function (html) {
console.log(html);
//$('#add').val('data sent');
//$('#msg').html(html);
$('#pprice').html("Price: $"+html);
}
});
}
}
回答by Basheer AL-MOMANI
I did that with helping from this question
我在这个问题的帮助下做到了这一点
jquery get querystring from URL
so let see how we will use this function
所以让我们看看我们将如何使用这个功能
// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
and now just use it in Ajax call
现在只需将其用于 Ajax call
"ajax": {
url: '/Departments/GetAllDepartments/',
type: 'GET',
dataType: 'json',
data: getUrlVars()// here is the tricky part
},
thats all, but if you want know how to use this function
or not send all the query string parameters
back to actual answer
就是这样,但如果你想知道how to use this function
或not send all the query string parameters
回到实际答案
回答by user3598298
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "ChnagePassword.aspx/AutocompleteSuggestions",
data: "{'searchstring':'" + request.term + "','st':'Arb'}",
dataType: "json",
success: function (data) {
response($.map(data.d, function (item) {
return { value: item }
}))
},
error: function (result) {
alert("Error");
}
});