【发布时间】:2017-04-11 18:15:09
【问题描述】:
谁能告诉我为什么 WebMethod 不能单独在一个页面中触发,相同的代码在另一个页面中有效,但在我希望它工作的页面中无效。我将整个代码转移到一个新页面中,它在那里工作得很好,但是如果我在我的实际页面中使用它,它不会触发 webmethod。不知道发生了什么。
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.0.min.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/jquery-ui.min.js" type="text/javascript"></script>
<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/themes/blitzer/jquery-ui.css" rel="Stylesheet" type="text/css" />
<script type="text/javascript">
$(function () {
$("[id$=txtSkill]").autocomplete({
source: function (request, response) {
$.ajax({
url: '<% =ResolveUrl("HRMCareerEAF.aspx/GetSkills") %>',
data: "{ 'prefix': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
return {
label: item.split('-')[0],
val: item.split('-')[1]
}
}))
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
select: function (e, i) {
$("[id$=hfSkillID]").val(i.item.val);
},
minLength: 1
});
});
</script>
<asp:TextBox ID="txtSkill" runat="server" style="text-align: center" />
[WebMethod(EnableSession = true)]
public static string[] GetSkills(string prefix)
{
HRMRecruitmentProcessDAL obj = new HRMRecruitmentProcessDAL();
DataSet ds = obj.BindMstcommon(HttpContext.Current.Session["CandidateID"].ToString(), "GetSkillsDD", "%" + prefix + "%");
List<string> skills = new List<string>();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
skills.Add(string.Format("{0}-{1}", ds.Tables[0].Rows[i]["Skill_Desc"].ToString(), ds.Tables[0].Rows[i]["skill_id"].ToString() + "|" + ds.Tables[0].Rows[i]["Skill_GroupID"].ToString())); //ds.Tables[0].Rows[i]["Skill_GroupDesc"].ToString() + " : " +
}
return skills.ToArray();
}
【问题讨论】:
-
服务器或浏览器有错误吗? “不工作”并没有说太多 - 显然它不起作用,或者你不会问问题
-
向我们显示警报中的错误。您可能希望在调试的每个步骤中都设置警报
-
自动完成不会显示在我的文本框下方,但它会显示我是否将此代码转移到新页面。当我尝试调试时,我了解到当我在文本框中输入内容时,我的 webmethod 没有被调用。
-
@PrasanthKumar 是您的 Jquerymethod 调用吗?你也试过在没有 ResolveUrl 的情况下使用吗?
-
如何在没有 Resolveurl 的情况下尝试,我是 jqery 的新手。在页面加载时 jquerymethod 被调用。
标签: javascript c# jquery asp.net ajax