1
對不起,發佈。我在這裏看到很多答案,並且還有this great post,但我的電話仍然無法正常工作。下面是我的配置:爲什麼我的asp mvc ajax GET請求有多個參數不起作用?
路由配置
routes.MapRoute(
name: "MyRoute",
url: "{controller}/{action}/{id}/{name}/{age}",
defaults: new { controller = "Home", action = "MyAction"
, id = UrlParameter.Optional
, name = UrlParameter.Optional
, age = UrlParameter.Optional}
);
操作方法在家庭控制器
[HttpGet]
public ActionResult MyAction(int id, string name, int age)
{
// Do some work and return View()
}
我的JavaScript代碼
function MyFunction(id)
{
var name = document.getElementById('name').value;
var age = document.getElementById('age').value;
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: {
"id": id,
"name": name,
"age": age
},
// url: '@Url.Action("MyAction", "Home")',
url: '/Home/MyAction',
success: function (response) {
alert("success");
},
error: function (xhr, status, error) {
alert("error");
}
});
}
此代碼失敗...它總是去error
和警報框一直顯示錯誤。它永遠不會去MyAction
行動Home
控制器無論我用戶的url
格式。
發生了什麼事?
真棒鬼鮫。它的工作....但它仍然調用錯誤功能。我不知道爲什麼。該行動已成功執行,但我的數據庫已正確更新......但'返回查看(模型)'失敗....我想這是別的。謝謝 。 – Bloomberg58
@ Bloomberg58,你的肌動蛋白質代碼是什麼? 我想你一定要用「return Json(model,JsonRequestBehavior.AllowGet);」 – Kisame
我想我應該這樣做。我正在做'return Json(「mystring」)或'return null'。在環顧四周之後,我選擇了返回一個簡單的'string'並且不在'$ .ajax'中指定'dataType'。這解決了我的問題。 – Bloomberg58