我正在尝试执行以下自动完成代码,但是当我调试/执行代码时。我注意到它没有按我的网络方法。
我的ASPX代码
<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>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
代码背后的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");
return listCt.ToArray();
}