這讓我感到非常緊張。我試着解決這兩天已經瀏覽了很多網頁。沒有得到ASP響應與JQuery AutoComplete一起工作
我想使用jquery-ui自動完成功能,正如http://jqueryui.com/resources/demos/autocomplete/remote-jsonp.html中的示例一樣,但不是調用search.php,而是需要調用ASP文件(Microsoft IIS環境)。因此,我創建了上述HTML文件的本地副本,並僅調整了URL。
$(function() {
function log(message) {
$("<div>").text(message).prependTo("#log");
$("#log").scrollTop(0);
}
$("#birds").autocomplete({
source: function(request, response) {
$.ajax({
url: "http://jqueryui.com/resources/demos/autocomplete/search.php",
dataType: "jsonp",
data: {
term: request.term
},
success: function(data) {
response(data);
}
});
},
minLength: 2,
select: function(event, ui) {
log("Selected: " + ui.item.value + " aka " + ui.item.id);
}
});
});
.ui-autocomplete-loading {
background: white url("http://jqueryui.com/resources/demos/autocomplete/images/ui-anim_basic_16x16.gif") right center no-repeat;
}
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Autocomplete - Remote JSONP datasource</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<body>
<div class="ui-widget">
<label for="birds">Birds: </label>
<input id="birds">
</div>
<div class="ui-widget" style="margin-top:2em; font-family:Arial">
Result:
<div id="log" style="height: 200px; width: 300px; overflow: auto;" class="ui-widget-content"></div>
</div>
</body>
- 現在,當我設置的URL的search.php中jquery.com(跨域,從我的Web服務器的文件,該文件也被原來的HTML)它工作有點慢,但很好
- 當我嘗試用我的本地ASP文件從SQL中讀取數據時,它不起作用,儘管當我直接在瀏覽器中調用該文件時,它將返回有效的JSON數據... I甚至改變了ASP文件,只做了一個
Response.Write
和硬編碼的JSON數據 - 我也
Response.ContentType = "application/json"
試了一下,但這並沒有幫助 - 然後我裝一個自己的search.php文件,該文件也只做一些硬編碼的JSON數據
echo
到https://softd4u.ch/search.php並試圖調用此方法,但同樣沒有成功
我在互聯網上發現了幾個樣本,ASP文件將直接作爲源參數(通常是較舊的帖子)輸入。我想這可能會起作用,但我認爲通過AJAX調用該文件是有道理的。
試圖調試jquery-1.12.4.js我至少發現有一個'parseerror'在某一點返回,但我不知道爲什麼和我應該做什麼不同。在function ajaxHandleResponses(s, jqXHR, responses)
變量響應是不確定的,但它有效的情況下,但它沒有工作時的情況下。
我確定文件實際上被調用(檢查日誌等),但他們從未觸發AJAX調用的成功功能。
有沒有人有一個想法,我在這裏失蹤?經典asp文件的
源 - 版本1(DB連接等在INC文件處理):傳統的ASP文件的
<!-- #INCLUDE FILE="includes/general.inc" -->
<%
Dim errortextInternal, rs, search, sql
Dim coma
search = Request.QueryString("term")
sql = "SELECT TOP 20 itmId, itmNo, itmDescr1, itmDescr2, itmInact, CASE WHEN itsuSupId1 IS NULL THEN '' ELSE itsuSupId1 END AS SupId1, " & _
"CASE WHEN itsuSupId2 IS NULL THEN '' ELSE itsuSupId2 END AS SupId2, " & _
"CASE WHEN itsuSupId3 IS NULL THEN '' ELSE itsuSupId3 END AS SupId3 " & _
"FROM Items LEFT JOIN ItemsSuppl ON itmId = itsuItmId WHERE "
sql = sql & " (itmNo LIKE '%" & search & "%' OR itmDescr1 LIKE '%" & search & "%' OR itmDescr2 LIKE '%" & search & "%' OR EXISTS " & _
"(SELECT * FROM ItemsSuppl AS sub WHERE sub.itsuItmId = itmId AND (itsuSupId1 LIKE '%" & search & "%' OR itsuSupId2 LIKE '%" & search & "%' OR itsuSupId3 LIKE '%" & search & "%'))) ORDER BY itmNo, itmDescr1 "
'Response.ContentType = "application/json"
Response.Write("[")
coma = ""
Success = SelectDbRecords(errortextInternal, rs, sql)
If Success Then
Do While Not rs.EOF
Response.Write(coma & "{""id"":""" & rs("itmId") & """,""label"":""" & Server.HTMLEncode(rs("itmNo") & " - " & Trim(Trim(rs("itmDescr1")) & " " & Trim(rs("itmDescr2")))) & """,""value"":""" & Server.HTMLEncode(rs("itmNo") & " - " & Trim(Trim(rs("itmDescr1")) & " " & Trim(rs("itmDescr2")))) & """}")
If coma = "" Then
coma = ","
End If
rs.MoveNext
Loop
rs.ActiveConnection.Close
Else
Response.Write("Fehler!")
End If
Response.Write("]")
%>
源 - 版本2(用於測試目的的硬編碼響應;無需檢索查詢字符串):
<%@LANGUAGE="VBSCRIPT"%>
<%
Response.Write("[{""id"":""Nycticorax nycticorax"",""label"":""Black-crowned Night Heron"",""value"":""Black-crowned Night Heron""},{""id"":""Ardea purpurea"",""label"":""Purple Heron"",""value"":""Purple Heron""},{""id"":""Tetrao tetrix"",""label"":""Black Grouse"",""value"":""Black Grouse""},{""id"":""Caprimulgus europaeus"",""label"":""European Nightjar"",""value"":""European Nightjar""},{""id"":""Picus viridis"",""label"":""European Green Woodpecker"",""value"":""European Green Woodpecker""},{""id"":""Saxicola rubicola"",""label"":""European Stonechat"",""value"":""European Stonechat""},{""id"":""Luscinia svecica"",""label"":""Bluethroat"",""value"":""Bluethroat""},{""id"":""Ardea cinerea"",""label"":""Grey Heron"",""value"":""Grey Heron""},{""id"":""Corvus cornix"",""label"":""Hooded Crow"",""value"":""Hooded Crow""},{""id"":""Sylvia curruca"",""label"":""Lesser Whitethroat"",""value"":""Lesser Whitethroat""},{""id"":""Pluvialis apricaria"",""label"":""European Golden Plover"",""value"":""European Golden Plover""},{""id"":""Sylvia communis"",""label"":""Common Whitethroat"",""value"":""Common Whitethroat""}]")
%>
我建議把jquery ui autosuggest換成更輕的解決方案,你可以自己調整。如果你喜歡,我可以發佈一個包含樣本的答案。 –
該示例使用[JSONP](http://stackoverflow.com/a/3840118/692942),你確定這是你想要做的嗎?如果是這樣,這裏是如何發送回調 - [使用CLASSIC ASP將數據返回給jsonp調用](http://stackoverflow.com/a/33498470/692942)。可能是目前JQuery沒有從你的頁面得到正確的響應,所以永遠不會觸及'success'處理程序,你是否嘗試過包括一個'fail'處理程序,甚至是'done'作爲一個catch-all? Ref [The jqXHR Object](http://api.jquery.com/jQuery.ajax/#jqXHR)。 – Lankymart
ASP或PHP元素只是一個服務器端腳本。 jQuery UI自動完成將通過'GET'發送一個'term'到定義的'url'。所以它和ASP以及PHP一起工作良好。請編輯您的帖子幷包含您的ASP代碼,以便我們瞭解腳本對發送給它的文本的處理方式,以及它返回給您的AJAX呼叫的數據。 – Twisty