我无法使用[WebMethod]
从Master Page
呼叫Jquery Ajax
。
我收到如下错误:
GetCompletionList(禁止)
我在具有母版页的 Default.aspx 网页的Jquery
的{{1}}中具有以下代码:
markup
在母版页的隐藏代码中, Mymasterpage.master.cs 我有这个:
<script type="text/javascript">
function ShowImage() {
document.getElementById('txSearch')
.style.backgroundImage = 'url(/aspnet/img/snake_transparent.gif)';
document.getElementById('txSearch')
.style.backgroundRepeat = 'no-repeat';
document.getElementById('txSearch')
.style.backgroundPosition = 'right';
}
function HideImage() {
document.getElementById('txSearch')
.style.backgroundImage = 'none';
}
$(function () {
$("[id$=txSearch]").autocomplete({
source: function (request, response) {
$.ajax({
url: '<%=ResolveUrl("Mymasterpage.master/GetCompletionList") %>',
data: "{ 'prefixText': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
return {
label: item.toString,
val: item.toString
}
}))
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
select: function (e, i) {
$("[id$=hfSearch]").val(i.item.val);
},
minLength: 1
});
});
</script>
为什么会这样?
如何解决?
谢谢
答案 0 :(得分:1)
在新的Web表单文件Default.aspx.cs
中插入Web方法,然后在Default.aspx
页中粘贴以下代码。
我希望我能帮上忙。
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<link rel="Stylesheet" href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/themes/redmond/jquery-ui.css" />
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.22/jquery-ui.js"></script>
<script type="text/javascript">
$(function () {
$("#<%=Textbox1.ClientID%>").autocomplete({
source: function (request, response) {
var param = { prefixText: $('#<%=Textbox1.ClientID%>').val() };
$.ajax({
url: "Default.aspx/GetCompletionList",
data: JSON.stringify(param),
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
dataFilter: function (data) { return data; },
success: function (data) {
response(data.d);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest);
}
});
},
minLength: 2 //minLength as 2, it means when ever user enter 2 character in TextBox the AutoComplete method will fire and get its source data.
});
});
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:TextBox runat="server" ID="Textbox1" />
</asp:Content>
答案 1 :(得分:0)
尝试使用Code Nugget Syntax <%:%>
例如:
Site.master.cs
public DateTime GetCurrentDate()
{
return DateTime.Now;
}
Site.master
<script>
function GetCurrentDate() {
var _date = "<%: GetCurrentDate() %>";
alert(_date);
}
</script>
这是我使用网站主方法的形式