jquery ui 自动完成需要额外的功能
我有这样的自动完成代码:
$("input#PickupSpot").autocomplete({
source: function(request, response){
$.ajax({
url: "AjaxSearch.aspx",
dataType: "jsonp",
data: {
a: "getspots",
c: "updateSpotList",
q: request.term
},
success: function(){
alert("Success");
},
error: function (xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);
}
});
}
});
当我尝试获取数据时,Firebug 显示错误:“updateSpotList 未定义”。 我需要创建一个名为 updateSpotList 的函数来获取服务器的响应。并且永远不会调用成功警报。
为什么我需要这个功能?也许aspx中定义了一些东西?这是:
string response = "";
string callback = Request.QueryString["c"];
string action = Request.QueryString["a"];
string query = Request.QueryString["q"];
//Ensure action parameter is set
if(action != null && action.Equals("getspots")){
//Ensure query parameter is set
if (query != null && query.Length > 0){
SpotListRequest request = new SpotListRequest
{
FreeText = query,
Language = "sv-SE"
};
IEnumerable<Spot> spots = DataBridge.GetSpotList(null, null, query).OrderBy(i => i.FullName);
JavaScriptSerializer js = new JavaScriptSerializer();
string json = js.Serialize(spots.ToArray());
//Ensure callback parameter is set
if (callback != null && callback.Length > 0)
{
response = String.Format("{0}('{1}')", callback, json);
}
}
}
I have such autocomplete code:
$("input#PickupSpot").autocomplete({
source: function(request, response){
$.ajax({
url: "AjaxSearch.aspx",
dataType: "jsonp",
data: {
a: "getspots",
c: "updateSpotList",
q: request.term
},
success: function(){
alert("Success");
},
error: function (xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);
}
});
}
});
When i try to get data Firebug shows an error: "updateSpotList is not defined".
I need to creat a function called updateSpotList to get response from the server. And the success alert is never invoked.
Why do i need this function? Maybe there are something defined in aspx? It is:
string response = "";
string callback = Request.QueryString["c"];
string action = Request.QueryString["a"];
string query = Request.QueryString["q"];
//Ensure action parameter is set
if(action != null && action.Equals("getspots")){
//Ensure query parameter is set
if (query != null && query.Length > 0){
SpotListRequest request = new SpotListRequest
{
FreeText = query,
Language = "sv-SE"
};
IEnumerable<Spot> spots = DataBridge.GetSpotList(null, null, query).OrderBy(i => i.FullName);
JavaScriptSerializer js = new JavaScriptSerializer();
string json = js.Serialize(spots.ToArray());
//Ensure callback parameter is set
if (callback != null && callback.Length > 0)
{
response = String.Format("{0}('{1}')", callback, json);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以指定 URL 作为源参数,而不是数据参数。
http://jqueryui.com/demos/autocomplete/#option-source
该插件将向您的服务器发出请求,您应该返回一个如下所示的 JSON 数组:
[{label: "Name"}, {label: "Name 2"}]
将您的 JavaScript 代码更改为:
自动完成插件将附加一个将名为 term 的参数添加到源 URL,并使用输入元素的当前值,因此如果您在输入元素中编写了 test,则将发出请求:“AjaxSearch.aspx?a=getspots&c=updateSpotList&term=test” 。
在服务器上,您希望将 q 更改为 term。
You can specify a URL as the source parameter, instead of the data parameter.
http://jqueryui.com/demos/autocomplete/#option-source
The plugin will make a request to your server, and you should return a JSON array looking like this:
[{label: "Name"}, {label: "Name 2"}]
Change your JavaScript code to this:
The autocomplete plugin will append a parameter named term to the source URL, with the current value of the input element, so a request would be made to: "AjaxSearch.aspx?a=getspots&c=updateSpotList&term=test" if you wrote test in the input element.
On the server, you want to change q to term.