[英].NET MVC posting multiple parameters to controller using ajax
我知道這個問題已經被問過很多次了,我確實嘗試過很多例子,但每次都因為未知的原因而失敗。 因此,我將向您展示我的示例(非常簡單的示例),我需要有人告訴我我做錯了什么。
從控制器(它的名字是 Recherche)方法開始:
public int getNote(string a,string b)
{
if(string.IsNullOrEmpty(a))
return 1;
else return 0;
}
正如你所看到的,我沒有使用變量 b,但誰在乎它只是一個例子。
現在對於ajax方法:
$.ajax({
type: "POST",
url: "/Recherche/getNote",
coententType: 'application/json',
dataType: 'json',
data:JSON.stringify({a:"a",b:"b"}),
success: successFunc,
});
function successFunc(data) {
document.getElementById('note').innerHTML = data;}
嘗試這個
var a1='';
var b1='';
$.ajax({
type: "POST",
url: "/Recherche/getNote",
coententType: 'application/json',
dataType: 'json',
data:JSON.stringify({a:a1,b:b1}),
contentType: "application/json; charset=utf-8",
processData: false,
success: function (data) {
document.getElementById('note').innerHTML = data;
},
error: function (response) {
if (response != 1) {
alert("Error!!!!");
}
}
});
控制器
[HttpPost]
[WebMethod]
public ActionResult getNote(string a,string b)
{
if (a== null && b==null) return Json(0);
//Some Action send Result
return Json(data, JsonRequestBehavior.AllowGet);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.