MVC4 ajax call and return data properly

hina10531

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.

Satpal

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MVC4 render image with Ajax call

From Dev

How to receive data sent using ajax call by Post method in MVC4?

From Dev

why do I have to match the parameter name to get json data from ajax call in MVC4 web app?

From Dev

Ajax call not functioning properly in ASP MVC

From Dev

Ajax call not functioning properly in ASP MVC

From Dev

How to call error function in $.ajax with c# MVC4?

From Dev

Json not parsing correctly from jQuery AJAX call in MVC4

From Dev

MVC4 @Html.Checkboxfor issue with Ajax call

From Dev

Call an Action method using Ajax in MVC4

From Dev

mvc4 Razor Ajax Call to show partial view

From Java

jQuery: Return data after ajax call success

From Dev

AJAX call and getting return data in Node

From Dev

Return data back to ajax call in jquery

From Dev

AJAX call and getting return data in Node

From Dev

unable to show return data via ajax call

From Dev

Return a subset of data based on current user role (MVC4)

From Dev

Return a subset of data based on current user role (MVC4)

From Dev

Jquery passing data to ajax function in mvc4

From Dev

save ajax return data to an array and call that array outside of ajax

From Dev

Ajax call to MVC Controller return "NOT FOUND"

From Dev

MVC4 Unit test NSubstitute Could not find a call to return from

From Dev

About node.js return data to ajax call function

From Dev

return data coming through an ajax call from a javascript function

From Dev

AJAX call return success message but post data never insert into db

From Dev

my json ajax call return me nothing in the callback, but the data in network

From Dev

Jquery Ajax Call Return

From Dev

return ajax by call back?

From Dev

How to pass a complex view model into a controller action via ajax call with JSON in .Net MVC4?

From Dev

Return List<T> from controller to ajax in asp.net mvc4

Related Related

  1. 1

    MVC4 render image with Ajax call

  2. 2

    How to receive data sent using ajax call by Post method in MVC4?

  3. 3

    why do I have to match the parameter name to get json data from ajax call in MVC4 web app?

  4. 4

    Ajax call not functioning properly in ASP MVC

  5. 5

    Ajax call not functioning properly in ASP MVC

  6. 6

    How to call error function in $.ajax with c# MVC4?

  7. 7

    Json not parsing correctly from jQuery AJAX call in MVC4

  8. 8

    MVC4 @Html.Checkboxfor issue with Ajax call

  9. 9

    Call an Action method using Ajax in MVC4

  10. 10

    mvc4 Razor Ajax Call to show partial view

  11. 11

    jQuery: Return data after ajax call success

  12. 12

    AJAX call and getting return data in Node

  13. 13

    Return data back to ajax call in jquery

  14. 14

    AJAX call and getting return data in Node

  15. 15

    unable to show return data via ajax call

  16. 16

    Return a subset of data based on current user role (MVC4)

  17. 17

    Return a subset of data based on current user role (MVC4)

  18. 18

    Jquery passing data to ajax function in mvc4

  19. 19

    save ajax return data to an array and call that array outside of ajax

  20. 20

    Ajax call to MVC Controller return "NOT FOUND"

  21. 21

    MVC4 Unit test NSubstitute Could not find a call to return from

  22. 22

    About node.js return data to ajax call function

  23. 23

    return data coming through an ajax call from a javascript function

  24. 24

    AJAX call return success message but post data never insert into db

  25. 25

    my json ajax call return me nothing in the callback, but the data in network

  26. 26

    Jquery Ajax Call Return

  27. 27

    return ajax by call back?

  28. 28

    How to pass a complex view model into a controller action via ajax call with JSON in .Net MVC4?

  29. 29

    Return List<T> from controller to ajax in asp.net mvc4

HotTag

Archive