返回JsonConvert.SerializeObject(JSON)后,在javascript上解析JSON。净处理程序)

时间:2022-10-29 21:01:40

i'm developing an web application in .net platform.

我正在。net平台上开发一个web应用程序。

i have wrote an Handler code that return a JSON object to Javascript (after i request in AJAX).

我已经编写了一个处理程序代码,它将JSON对象返回到Javascript(在我请求AJAX之后)。

the Handler code:

处理程序代码:

var wrapper = new { 
    left = left.ToString(), 
    top = top.ToString(), 
    width = width.ToString(), 
    height = height.ToString() };
context.Response.Write(JsonConvert.SerializeObject(wrapper));

In Javascript, when i do alert, i see that i get an object. and it's good.
But now i want to parse it to JSON.

在Javascript中,当我发出警报时,我看到有一个对象。它很好。现在我想把它解析为JSON。

when i do JSON.parse(msg); i get an error

当我做JSON.parse(味精);我得到一个错误

"JSON.parse: unexpected character"

“JSON。解析:意想不到的角色”

when i do jQuery.parseJSON(msg); using jquery-1.6.2, i get this error

当我做jQuery.parseJSON(味精);使用jquery-1.6.2,我得到这个错误

jQuery.parseJSON is not a function (i'm using jquery-1.6.2)

jQuery。parseJSON不是函数(我使用的是jquery-1.6.2)

What is the problem?

这个问题是什么?

1 个解决方案

#1


2  

Try this.

试试这个。

Create a page called TestPage.aspx like this.

创建一个名为TestPage的页面。aspx这样的。

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Test Page</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $.ajax({
                url: 'TestPage.aspx/GetDimensions',
                type: 'POST',
                contentType: 'application/json',
                data: '{}',
                success: function (response) {
                    // Don't forget that the response is wrapped in a
                    //  ".d" object in ASP.NET 3.5 and later.
                    var data = response.d;
                    $('#test-div').animate({
                        left: data.left + 'px',
                        top: data.top + 'px',
                        height: data.height + 'px',
                        width: data.width + 'px'
                    }, 5000, function () {
                        // Animation complete.
                    });
                }
            });
        });
    </script>
    <style type="text/css">
        #test-div
        {
            background-color:#eee;
            border: 1px solid #ccc;
            border-radius: 5px;
            height: 100px;
            left:0px;
            padding-top: 40px;
            text-align:center;
            top:0px;
            width: 100px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">

    <div id="test-div">
    This is a test div
    </div>

    </form>
</body>
</html>

And on TestPage.aspx.cs, do this

TestPage.aspx。cs,做这个

using System.Web.Services;

public partial class Test1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e){/*page load eent*/}

    static int left = 50;
    static int top = 50;
    static int height = 200;
    static int width = 200;

    [WebMethod]
    public static object GetDimensions()
    {
        return new
        {
            left = left.ToString(),
            top = top.ToString(),
            width = width.ToString(),
            height = height.ToString()
        };
    }
}

Hope this helps.

希望这个有帮助。

Courtesy: ASP.NET web services mistake: manual JSON serialization by Dave Ward

礼貌:ASP。NET web服务错误:Dave Ward的手工JSON序列化

#1


2  

Try this.

试试这个。

Create a page called TestPage.aspx like this.

创建一个名为TestPage的页面。aspx这样的。

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Test Page</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $.ajax({
                url: 'TestPage.aspx/GetDimensions',
                type: 'POST',
                contentType: 'application/json',
                data: '{}',
                success: function (response) {
                    // Don't forget that the response is wrapped in a
                    //  ".d" object in ASP.NET 3.5 and later.
                    var data = response.d;
                    $('#test-div').animate({
                        left: data.left + 'px',
                        top: data.top + 'px',
                        height: data.height + 'px',
                        width: data.width + 'px'
                    }, 5000, function () {
                        // Animation complete.
                    });
                }
            });
        });
    </script>
    <style type="text/css">
        #test-div
        {
            background-color:#eee;
            border: 1px solid #ccc;
            border-radius: 5px;
            height: 100px;
            left:0px;
            padding-top: 40px;
            text-align:center;
            top:0px;
            width: 100px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">

    <div id="test-div">
    This is a test div
    </div>

    </form>
</body>
</html>

And on TestPage.aspx.cs, do this

TestPage.aspx。cs,做这个

using System.Web.Services;

public partial class Test1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e){/*page load eent*/}

    static int left = 50;
    static int top = 50;
    static int height = 200;
    static int width = 200;

    [WebMethod]
    public static object GetDimensions()
    {
        return new
        {
            left = left.ToString(),
            top = top.ToString(),
            width = width.ToString(),
            height = height.ToString()
        };
    }
}

Hope this helps.

希望这个有帮助。

Courtesy: ASP.NET web services mistake: manual JSON serialization by Dave Ward

礼貌:ASP。NET web服务错误:Dave Ward的手工JSON序列化