C# jQuery自动完成在托管站点上不起作用
我正在使用以下代码自动完成我的网站中的搜索文本框。代码在localhost上运行良好,但是当站点被托管时,“错误”框会弹出 脚本:C# jQuery自动完成在托管站点上不起作用,c#,javascript,jquery,jquery-autocomplete,C#,Javascript,Jquery,Jquery Autocomplete,我正在使用以下代码自动完成我的网站中的搜索文本框。代码在localhost上运行良好,但是当站点被托管时,“错误”框会弹出 脚本: $(document).ready(function () { SearchText(); }); function SearchText() { $(".autosuggest").autocomplete({ source: function (request, response) { $.ajax({
$(document).ready(function () {
SearchText();
});
function SearchText() {
$(".autosuggest").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Index.aspx/GetAutoCompleteData",
data: "{'location':'" + document.getElementById('ContentPlaceHolder1_txtSearch').value + "'}",
dataType: "json",
success: function (data) {
response(data.d);
},
error: function (result) {
alert("Error");
}
});
}
});
}
功能代码为:
[WebMethod]
public static List<string> GetAutoCompleteData(string location)
{
List<string> result = new List<string>();
string connect = @"
Data Source=jaipurrooms.db.11458954.hostedresource.com; Initial Catalog=jaipurrooms;
User ID=xyz;
Password=xyz;";
using (SqlConnection con = new SqlConnection(connect))
{
using (SqlCommand cmd = new SqlCommand("select DISTINCT Location from Details where Location LIKE '%'+@location+'%'", con))
{
con.Open();
cmd.Parameters.AddWithValue("@location", location);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(dr["Location"].ToString());
}
return result;
}
}
}
[WebMethod]
公共静态列表GetAutoCompleteData(字符串位置)
{
列表结果=新列表();
字符串连接=@“
数据源=jaipurrooms.db.11458954.hostedresource.com;初始目录=jaipurrooms;
用户ID=xyz;
密码=xyz;“;
使用(SqlConnection con=newsqlconnection(connect))
{
使用(SqlCommand cmd=new SqlCommand(“从详细信息中选择不同的位置,如“%”++@Location++“%”,con))
{
con.Open();
cmd.Parameters.AddWithValue(“@location”,location);
SqlDataReader dr=cmd.ExecuteReader();
while(dr.Read())
{
添加(dr[“Location”].ToString());
}
返回结果;
}
}
}
我认为您必须在ajax请求中检查url。请尝试使用以下语法:
url: "./Index.aspx/GetAutoCompleteData",
为什么不使用完整的URL呢?您可以使用fiddler2捕获ajax调用的请求和响应。