0

I am trying to do an ajax call from my view where I return a json object from my controller. However, when I try this I get the object type, but not the values as strings. Here is my code..

Controller

public class HomeController : Controller
{
    //
    // GET: /Home/

    public ActionResult Index()
    {
        return View();
    }

    public ActionResult GetPerson()
    {
        var _model = _person;
        return Json(_model, JsonRequestBehavior.AllowGet);
    }

    static Person _person = new Person()
    {
        FirstName = "Steve",
        LastName = "Johnson",
        Age = 27
    };  
}

View

@{
Layout = null;
}

<!DOCTYPE html>

<html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
        <script src="~/Scripts/jquery-2.0.3.min.js"></script>
    </head>
    <body>
        <div>
            <form>
                <fieldset>
                    <legend>The Person</legend>
                    <label>Name: </label>
                    <input />
                    <label>Age: </label>

                </fieldset>

            </form>
        </div>
    <script type="text/javascript">
        $(document).ready(function() {
            $.ajax({
            url: '/Home/GetPerson',
            type: 'GET',
            success: function (result) { alert(result);}
        });
    });
    </script>

The alert box return [object Object]. I am trying to get the string values of the "Person" object.

Any help would be appreciated.

Thanks!

4

3 回答 3

2

您没有从您 ddet 的警报中获取数据以指定您尝试警报的变量

 success: function (result) { alert(result.FirstName);}
于 2013-09-06T02:41:46.530 回答
1

success: function (result) { alert(result[0].FirstName);} 当返回一个 json 结果值时,你应该在字段名后面加上索引号

于 2013-09-06T03:27:12.590 回答
1

尝试更改alert(result);alert(JSON.stringify(result); That way 您可以看到实际返回的 JSON 字符串。

于 2013-09-06T04:12:39.060 回答