Я пытаюсь сделать 2 отдельных вызова AJAX с помощью кнопок. Я хочу, чтобы произошло следующее: при нажатии кнопки «1» ProductsTable показывает данные из веб-службы; при нажатии кнопки 2 OthersTable показывает свои собственные данные из веб-службы. Но сейчас, когда нажата любая из кнопок, ничего не появляется. Я знаю, что код работает, если есть только один из них, и он не обернут вокруг функции .click.
Нет сообщений об ошибках. 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>