我如何使用 Jquery Ajax 调用母版页方法
How I Call Master Page Method Using Jquery Ajax
我无法使用 Jquery Ajax
从 Master Page
调用 [WebMethod]
。
我收到如下错误:
GetCompletionList (forbidden)
我在 Default.aspx 网页的 markup
上的 Jquery
中有以下代码 母版页:
<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>
在母版页的代码隐藏中Mymasterpage.master.cs我有这个:
[ScriptMethod()]
[WebMethod]
public static List<string> GetCompletionList(string prefixText)
{
using (OdbcConnection con =
new OdbcConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString))
{
using (OdbcCommand com =
new OdbcCommand())
{
com.CommandText = " SELECT ";
com.CommandText += " sName ";
com.CommandText += " FROM ";
com.CommandText += " `tbl_name` ";
com.CommandText += " WHERE ";
com.CommandText += " sName LIKE CONCAT('%',?,'%'); ";
com.Parameters.AddWithValue("param1", prefixText);
com.Connection = con;
con.Open();
List<string> countryNames = new List<string>();
using (OdbcDataReader sdr = com.ExecuteReader())
{
while (sdr.Read())
{
countryNames.Add(sdr["sName"].ToString());
}
}
con.Close();
return countryNames;
}
}
}
为什么会这样?
如何解决?
谢谢
在新的 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>
尝试使用 Code Nugget Syntax <%: %>
例如:
Site.master.cs
public DateTime GetCurrentDate()
{
return DateTime.Now;
}
Site.master
<script>
function GetCurrentDate() {
var _date = "<%: GetCurrentDate() %>";
alert(_date);
}
</script>
这是我用来使用网站主方法的表格
我无法使用 Jquery Ajax
从 Master Page
调用 [WebMethod]
。
我收到如下错误:
GetCompletionList (forbidden)
我在 Default.aspx 网页的 markup
上的 Jquery
中有以下代码 母版页:
<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>
在母版页的代码隐藏中Mymasterpage.master.cs我有这个:
[ScriptMethod()]
[WebMethod]
public static List<string> GetCompletionList(string prefixText)
{
using (OdbcConnection con =
new OdbcConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString))
{
using (OdbcCommand com =
new OdbcCommand())
{
com.CommandText = " SELECT ";
com.CommandText += " sName ";
com.CommandText += " FROM ";
com.CommandText += " `tbl_name` ";
com.CommandText += " WHERE ";
com.CommandText += " sName LIKE CONCAT('%',?,'%'); ";
com.Parameters.AddWithValue("param1", prefixText);
com.Connection = con;
con.Open();
List<string> countryNames = new List<string>();
using (OdbcDataReader sdr = com.ExecuteReader())
{
while (sdr.Read())
{
countryNames.Add(sdr["sName"].ToString());
}
}
con.Close();
return countryNames;
}
}
}
为什么会这样?
如何解决?
谢谢
在新的 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>
尝试使用 Code Nugget Syntax <%: %>
例如:
Site.master.cs
public DateTime GetCurrentDate()
{
return DateTime.Now;
}
Site.master
<script>
function GetCurrentDate() {
var _date = "<%: GetCurrentDate() %>";
alert(_date);
}
</script>
这是我用来使用网站主方法的表格