I'm trying to pass a dynamic, user created object through AJAX to some C#. I'm not really experienced with JSON, but it seemed like a good method. I'm not sure why, but it's giving me an error on my declaration of the object. (Supposedly.) What am I doing wrong? Thanks.
EDIT: It seems to only error in IE and 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, 28 Mar 2012 14:15:19 UTC
Message: Expected identifier, string 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 Behind
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; }
}