我试图通过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; }
}
什么对象的宣言?发布更详细的错误描述以及您需要帮助的地方。 – SkonJeet 2012-03-28 14:12:29