我是 Jquery 的新手。我在 aspx 页面上有一个文本框。当输入 2 个字母时,文本框使用 jquery ajax 从 sql server 数据库中获取土耳其邮政编码。但是如果有超过 900 条记录要从 sql server 返回,jquery 会给出“internal server error”。我认为将数据带到文本框需要更多时间,但 jquery 不允许这样做并给出内部错误。现在我希望在第一次请求时提供部分数据。例如; 如果用户在文本框中输入 34,ajax 将不会带来所有以 34 开头的数据。它会带来前 100 个结果。然后当用户将滚动条滑动到记录的末尾时,将查询新记录并显示给用户。就像脸书帖子一样。有可能吗?如果是这样,如果您与我们分享示例代码,我会很高兴。
Javascript
$(function() {
$(".tb").autocomplete
({
source: function(request, response) {
$.ajax({
type: "POST",
url: "WebService_GetData.asmx/GetCmbPostaKod",
dataType: "json",
//data: "{ 'filterKey': '" + request.term + "','select1': '" + $('#cmbLogisticType').val() + "' }",
data: JSON.stringify({filterKey: request.term, lojtype:$('#cmbLogisticType').val(), ulke:$asp('ulke').val()}),
contentType: "application/json; charset=utf-8",
dataFilter: function(data) { return data; },
success: function(data) {
response($.map(data.d, function(item) {
return { value: item.PostaKodu, label: item.SemtAdi + ' / ' + item.IlceAdi + ' / ' + item.IlAdi + ' - ' + item.PostaKodu }
}))
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
},
minLength: 2,
multiple: true,
matchContains: true,
formatItem: formatItem,
formatResult: formatResult
});
});
aspx.cs 背后的代码
[WebMethod]
public List<PostaKod> GetCmbPostaKod(string filterKey, string lojtype, string ulke)
{
string stmt;
DataTable dt;
//myToolKit.AddValueToCombo(cmbVergiDairesi, "0", "Seçiniz");
//myToolKit.AddValueToCombo(cmbSatisElemani, "0", "Seçiniz");
FunctionsToolkit.InjectionKontrolEt(ref filterKey);
mySqlKit.DoConnect();
/*
stmt = string.Format(@"SELECT ILADI, ILCEADI, SEMTADI, POSTAKODU, ULKEKODU FROM TBLPOSTAKODLARI (NOLOCK)
WHERE POSTAKODU LIKE '{0}%' AND ULKEKODU='{1}' ORDER BY POSTAKODU", filterKey, (lojtype=="3")?ulke:lojtype);
*/
stmt = string.Format(@"EXEC sp_get_postakodu '{0}','{1}'",filterKey,(lojtype=="3")?ulke:lojtype);
dt = mySqlKit.DoDTstr(stmt);
mySqlKit.DoDisconnect();
return mySqlKit.ComboDList(dt);
}
页面 aspx
<div id="panel1">
Nakliye Tipini Seçiniz* :
<select name="LogisticType" id="cmbLogisticType" onchange="toggle(this.value)">
<option value="1" selected="selected"> Seçiniz </option>
<option value="90"> Şehiriçi </option>
<option value="90"> Şehirlerarası </option>
<option value="3"> Uluslararası </option>
</select>
<!--
<asp:DropDownList ID="cmbPostaKodu" name="cmbPostaKodu" runat="server" class="combo">
</asp:DropDownList>
<select ID="cmbIl" name="cmbIl" runat="server" onchange="ilceGetir(this.options[this.options.selectedIndex].value)" class="combo"></select>
<select ID="cmbIlce" name="cmbIlce" runat="server" class="combo"></select>-->
</div>
<div class="slidingDiv">
<div class="slidingDiv2">
Hareket Noktası (Ülke):
<div class="ui-widget">
<div class="text1">
<asp:TextBox ID="ulke" class="tb" runat="server" onfocus="this.value = ( this.value == this.defaultValue ) ? '' : this.value;return true;">Ülke Seçiniz</asp:TextBox>
</div>
</div>
Varış Noktası (Ülke):
<div class="ui-widget">
<div class="text1">
<asp:TextBox ID="ulke2" class="tb" runat="server" onfocus="this.value = ( this.value == this.defaultValue ) ? '' : this.value;return true;">Ülke Seçiniz</asp:TextBox>
</div>
</div>
</div>
Hareket Noktası (Posta Kodu):
<div class="ui-widget">
<div class="text1">
<asp:TextBox ID="city" class="tb" runat="server" onfocus="this.value = ( this.value == this.defaultValue ) ? '' : this.value;return true;">Posta Kodu Giriniz</asp:TextBox>
</div>
</div>
Varış Noktası (Posta Kodu):
<div class="ui-widget">
<div class="text1">
<asp:TextBox ID="city2" class="tb" runat="server" onfocus="this.value = ( this.value == this.defaultValue ) ? '' : this.value;return true;">Posta Kodu Giriniz</asp:TextBox>
</div>
</div>
</div>