不知道我在做什么错,但我无法让我的JQuery AJAX调用正确地传递变量。它收到它很好。我可能忽略了一些小事。谢谢。无法将变量从JQuery AJAX传递到后面的C#代码
(此外,有没有什么办法可以这种方式传递数据,而无需使用[的WebMethod]或通过网址?)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script src="Scripts/jquery-1.7.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$('#button').click(function(){
var json_obj = "{'" + $('#t1').val() + "' : '" + $('#p1').val() + "', '" + $('#t2').val() + "' : '" + $('#p2').val() + "'}";
$.ajax({
type: "POST",
url: 'Default.aspx/test',
contentType: 'application/json; charset=utf-8',
data: "thisisatest",//json_obj,
dataType: 'json',
success: function(msg) {
//$('#result').html(msg.d);
alert(msg.d)
},
error: function(msg) {
//$('#result').html(msg.d);
alert(msg.d + " err")
}
});
});
});
</script>
</head>
<body>
<div>
Type: 1: <input type="text" id="t1" />
Property 1: <input type="text" id="p1" />
<br /><br />
Type 2: <input type="text" id="t2" />
Property 2: <input type="text" id="p2" />
<input type="button" value="Add object!" id="button" />
<br /><br />
<div id="result"></div>
</div>
</body>
</html>
代码隐藏
[WebMethod]
public string test(string json)
{
return json;
}
第一:数据:“thisisatest”不是JSON 第二:显示您的代码如何接收和填充请求对象。 – Quintium 2012-03-28 16:03:16
我知道“thisisatest”不是JSON,我将JSON注释掉,以便用简单的字符串进行测试,因为它不起作用。不确定你对第二部分的含义。 – Ber53rker 2012-03-28 16:06:14