问题描述
我正在尝试执行以下自动完成代码,但是当我调试/执行代码时。我注意到它没有按我的网络方法。
ASPX代码
< html xmlns =http://www.w3.org/1999/xhtml>
< head runat =server>
< title>< / title>
< link href =Styles / jquery-ui-1.10.4.custom.min.css =stylesheettype =text / css/ >
< script src =Scripts / jquery-1.10.2.jstype =text / javascript>< / script>
< script src =Scripts / jquery-ui-1.10.4.custom.min.jstype =text / javascript>< / script>
< script type =text / javascriptlanguage =javascript>
$(document).ready(function(){
SearchText();
}) ;
函数SearchText(){
$('#<%= TextBox1.ClientID%>')。autocomplete({
source :function(request,response){
var param = {searchStr:$('#TextBox1')。val()};
$ .ajax({
类型:'POST',
contentType:application / json; charset = utf-8,
url:WebForm1.aspx / GetCountry ,
//数据:{searchStr:JSON.stringify('u')},
数据:{searchStr:'xx'},
//数据:JSON.stringify(param),
dataType:json,
成功:函数(数据){
回复(data.d);
},
错误:功能ion(msg,text){
alert(msg);
}
});
}
});
}
< / script>
< / head>
< body>
< form id =form1runat =server>
I am trying to execute the below code for AutoComplete, but when I debug/execute the code. I noticed itsnot hitting my web method.
ASPX Code
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="Styles/jquery-ui-1.10.4.custom.min.css" rel="stylesheet" type="text/css" />
<script src="Scripts/jquery-1.10.2.js" type="text/javascript"></script>
<script src="Scripts/jquery-ui-1.10.4.custom.min.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function () {
SearchText();
});
function SearchText() {
$('#<%=TextBox1.ClientID %>').autocomplete({
source: function (request, response) {
var param = { searchStr: $('#TextBox1').val() };
$.ajax({
type: 'POST',
contentType: "application/json; charset=utf-8",
url: "WebForm1.aspx/GetCountry",
// data: "{searchStr:JSON.stringify('u')}",
data: "{searchStr: 'xx'}",
//data: JSON.stringify(param),
dataType: "json",
success: function (data) {
response(data.d);
},
error: function (msg, text) {
alert(msg);
}
});
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
< ; asp:TextBox ID =TextBox1runat =server>
<asp:TextBox ID="TextBox1" runat="server">
< / form>
< ; / body>
< / html>
代码背后的C#代码
[WebMethod(EnableSession = true)]
[ScriptMethod(UseHttpGet = true,ResponseFormat = ResponseFormat.Json)]
public static string [] GetCountry(string searchStr)
{
List< string> listCt = new List< string>();
listCt.Add(UnitedStates);
listCt.Add(UnitedKingdom);
listCt.Add(UnitedArab);
listCt.Add(UnitedNations);
返回listCt.ToArray();
}
</form>
</body>
</html>
C# code behind code
[WebMethod(EnableSession = true)]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public static string[] GetCountry(string searchStr)
{
List<string> listCt = new List<string>();
listCt.Add("UnitedStates");
listCt.Add("UnitedKingdom");
listCt.Add("UnitedArab");
listCt.Add("UnitedNations");
return listCt.ToArray();
}
推荐答案
这篇关于WebMethod没有从ajax / jquery调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!