I am trying to pass a dynamic, user created object via AJAX to some C #. I am not very good at JSON, but it was a good method. I'm not sure why, but this gives me an error in my object declaration. (Presumably.) What am I doing wrong? Thanks.
EDIT: It seems that there is only an error in IE, but I need it to work in IE7.
Webpage Error Details
User agent: Mozilla / 4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; NET4.0C; .NET4.0E; MDDC; InfoPath. 2) Timestamp: Wed, Mar 28, 2012 2:15:19 PM UTC
Message: expected identifier, line or number Line: 18 Char: 21 Code: 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>
Code for
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; } }
source share