我正在尝试使用按钮进行2个单独的AJAX调用。我想要发生的是:单击Button1时ProductsTable显示来自web服务的数据;当Button2被点击时OthersTable从web服务中显示它自己的数据。但是现在,当点击任一按钮时,什么也没有显示出来。我知道代码是有效的,如果只有其中的一个,它不包围.click函数。如何在JQuery中进行多个但单独的AJAX调用
没有错误信息。 ASP.NET 4.0,JQuery 1.4.4。不使用ScriptManager。不使用UpdatePanels。下面
代码:
<script src="Scripts/jquery-1.4.4.js" type="text/javascript"></script>
<script src="Scripts/jquery.tmpl.js" type="text/javascript"></script>
<script id="ProductsTemplate" type="text/x-query-tmpl">
<tables id="ProductsTemplate">
<thead>
</thead>
<tbody>
{{each d}}
{{tmpl($value) '#ProductsRowTemplate'}}
{{/each}}
</tbody>
</tables>
</script>
<script id="ProductsRowTemplate" type="text/x-query-tmpl">
<tr>
<td>${title}</td>
<td>${size}</td>
<td>${price}</td>
</tr>
</script>
<script id="Products2Template" type="text/x-query-tmpl">
<tables id="Products2Template">
<thead>
</thead>
<tbody>
{{each d}}
{{tmpl($value) '#Products2RowTemplate'}}
{{/each}}
</tbody>
</tables>
</script>
<script id="Products2RowTemplate" type="text/x-query-tmpl">
<tr>
<td>${title}</td>
<td>${size}</td>
<td>${price}</td>
</tr>
</script>
<script type="text/javascript">
$(document).ready(function() {
$("#Button1").click(function() {
$.ajax({
type: "POST",
url: "JSON-WebService.asmx/getProducts",
data: "{}",
contentType: "application/json; charset=UTF-8",
dataType: "json",
success: function (data) {
$('#ProductsTemplate').tmpl(data).appendTo('#ProductsTable');
alert("Products works");
},
failure: function (data) {
alert("Products didn't work");
}
});
});
$("#Button2").click(function() {
$.ajax({
type: "POST",
url: "JSON-WebService.asmx/getProducts",
data: "{}",
contentType: "application/json; charset=UTF-8",
dataType: "json",
success: function (data) {
$('#Products2Template').tmpl(data).appendTo('#OthersTable');
alert("Products2 works");
},
failure: function (data) {
alert("Products2 didn't work");
}
});
});
});
</script>
<title>Using JQuery</title>
<form id="form1" runat="server">
<div id="ProductsTable">
</div>
<div id="OthersTable">
</div>
<div>
<asp:Button ID="Button1" runat="server" Text="Products" />
<asp:Button ID="Button2" runat="server" Text="Products2" />
</div>
</form>
这是奇怪的,我们可以在一个页面上有许多AJAX多个请求,我觉得有什么不对的后端,你可以观看萤火虫.NET面板的结果或有在后端的中断点 – kobe 2010-11-19 15:52:28
点击后,请求即将到来,但我可以从中看出这一点。我的语法错了吗? – dotnetN00b 2010-11-19 17:35:38
你呈现的HTML看起来像那些按钮的样子是什么? – 2010-11-20 00:48:56