I invoke an action in a controller using ajax like below.
$.ajax({
type: "POST",
url: "getUserInfo.json",
data: "",
success: function (data) {
if (data.resultInfo.result != "SUCCESS") { alert("error"); return; }
setUserInfo(data.userInfo);
//alert(data.resultInfo.result);
//alert(data.resultInfo.message);
settingMenu();
//historyBackProc(); //histroyback catch
},
error: function (data) {
alert(data.resultInfo.message);
}
});
And what it ends up calling is this action.
public ActionResult getUserInfo()
{
if ( Session["UserInfo"] != null ) {
ViewData.Add("resultInfo", new resultInfo("SUCCESS"));
ViewData.Add("UserInfo", Session["UserInfo"]);
return Json(ViewData);
}
else
{
return RedirectToAction("index.mon");
}
}
and back to the ajax success callback, the data isn't what I want it to be.
What I want is,
data
- "resultInfo" : object
- "UserInfo" : object
But it ended up having just objects, not keys.
data
- object[0]
- Key : "resultInfo"
- Value : object
- object[1]
- Key : "UserInfo"
- Value : object
To achieve this, how to manipulate the return object in the action? I need to do this to run this web application with the absolutely same javascript in both JAVA and .NET environment.
There is no need to use ViewData
when you are returning JSON. You are getting result since ViewData
is a dictionary.
Use
return Json(new {
resultInfo = new resultInfo("SUCCESS"),
UserInfo = Session["UserInfo"]
});
instead of
ViewData.Add("resultInfo", new resultInfo("SUCCESS"));
ViewData.Add("UserInfo", Session["UserInfo"]);
return Json(ViewData);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments