2011-07-17 14 views
2

私は.netプラットフォームでWebアプリケーションを開発しています。JsonConvert.SerializeObject(json)[.NETハンドラ]返信後にJSONを解析します。

私はAJAXでリクエストした後にJSONオブジェクトをJavascriptに返すHandlerコードを書いています。

ハンドラコード:私は警告行うときに、JavaScriptで

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

、私は、オブジェクトを取得していることがわかります。それは良いです。
しかし、今私はそれをJSONに解析したい。私はJSON.parse(msg);を行う際

私が使用してjQuery.parseJSON(msg);を行う際にjqueryの-1.6.2が、私はこのエラー

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

問題は何であるの誤差が出

"JSON.parse: unexpected character"

取得しますか?

答えて

2

これを試してください。

このような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> 

そして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() 
     }; 
    } 
} 

は、この情報がお役に立てば幸いです。

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

+2

JavaScriptSerializerが同様にASP.NET 3.5で提供されています。 –

+0

@Mehdi Golchin:MSDNを確認するにはあまりにも怠惰で、迷っていました。ありがとうございました。更新されました: – naveen

+0

ありがとうございます!病気にしてみてください:) – Idoshhh