2012-03-28 33 views
4

我試圖通過AJAX將動態的,用戶創建的對象傳遞給一些C#。我對JSON並沒有太多的經驗,但它似乎是一個很好的方法。我不知道爲什麼,但它給我在對象聲明上的錯誤。 (據說)。我在做什麼錯了?謝謝。嘗試使用AJAX和JQuery將JSON對象傳遞給C#

編輯:它似乎只在IE中的錯誤,但我需要它在IE7中工作。

網頁錯誤的詳細信息

用戶代理:Mozilla的/ 4.0(兼容; MSIE 7.0; Windows NT的6.1; WOW64; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0。 30729;媒體中心PC 6.0; .NET4.0C; .NET4.0E; MDDC; InfoPath.2) 時間戳:星期三,2012年3月28日14時15分十九秒UTC

消息:預期標識符,字符串或數字 行:18 Char:21 代碼:0 URI:http://localhost:56560/Default.aspx

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
    <title>Untitled Page</title> 
     <script type="text/javascript"> 
     $(function() { 

      $('input[type=button').click(function(){ 

       var json_obj = { $('#t1').val() : $('#p1').val(), 
          $('#t2').val() : $('#p2').val()}; 

       $.ajax({ 
        typeof: "POST", 
        url: '/test.aspx', 
        contentType: 'application/json; charset=utf-8', 
        data: json_obj, 
        dataType: 'json', 
        success: function(msg) { 
         alert('Success!'); 
        }, 
        error: function(msg) { 
         alert('Error!'); 
        } 
       }); 
      }); 
     }); 
    </script> 
</head> 
<body> 
    <div> 
     Type: 1: <input type="text" id="t1" /> 
     Property 1: <input type="text" id="p1" /> 

     Type 2: <input type="text" id="t2" /> 
     Property 2: <input type="text" id="p2" /> 
     <input type="button" value="Add object!" /> 
    </div> 
</body> 
</html> 

代碼隱藏

public class Test 
{ 
    public Test(string json) 
    { 
     JObject jObj = JObject.Parse(json); 
     JToken jUser = jObj["json_obj"]; 
     first = (string)jObj["t1"]; 
     second = (string)jObj["t2"]; 
    } 

    public string first { get; set; } 
    public string second { get; set; } 
} 
+1

什麼對象的宣言?發佈更詳細的錯誤描述以及您需要幫助的地方。 – SkonJeet 2012-03-28 14:12:29

回答

2

我覺得你JSO的格式n數據錯誤。試試這個:

var json_obj = "{'" + $('#t1').val() + "' : '" + $('#p1').val() + "', '" + $('#t2').val() + "' : '" + $('#p2').val() + "'}"; 
+0

這是JSON的字符串表示形式。這不是JSON _object_, – 2013-08-15 04:53:37

+2

或......他可以將json對象包裝爲JSON.stringify(json_obj)。這會更安全 – 2013-08-15 07:49:55

0

你可以在你的C#代碼添加的功能,如:

[HttpPost] 
public JsonResult Test() 
{ 
    return Json(new {Success = true, CustomJSONAttribute="Whatever You Like"}); 
} 

然後調整JQuery的阿賈克斯測試(點),然後在你的成功的功能你可以這樣做:

msg.Success和msg.CustomJSONAttribute

0

爲了什麼是值得的,我一直在爲此奮鬥了幾個小時。我最終通過確保我的$ .ajax調用中的JSON對象/ var與C#中的參數名稱匹配來解決缺少的參數問題。我真的不能相信這是問題所在。

[WebMethod] 
    public static void SetSession(String **json**) 
    { 
     String s = json; 
    } 

...

var json_obj = "{'" + '**json**' + "' : '" + 'the_val' + "'}"; 
       $.ajax({ 
        type: "POST", 
        url: "my_page.aspx/SetSession", 
        data: json_obj, 
        contentType: "application/json; charset=utf-8", 
        dataType: 'json', 
        success: function() 
        { 
         alert('SetSession executed.'); 
        }, 
        error: function (XMLHttpRequest, textStatus, errorThrown) 
        { 
         alert("Status: " + textStatus); alert("Error: " + XMLHttpRequest.responseText); 
        } 
       }); 
相關問題