This is driving me nuts. I've tried solving this two days already browsing through many webpages.
I'd like to use the jquery-ui autocomplete functionality just exactly as the sample in http://jqueryui.com/resources/demos/autocomplete/remote-jsonp.html, but instead of calling search.php I need to call an ASP file (Microsoft IIS environment). So, I created a local copy of above mentioned HTML file and adjusted only the 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>
- Now, when I set URL to search.php of jquery.com (cross-domain, from my web server to the file which is also used by the original HTML) it works a bit slow, but fine
- When I try it with my local ASP file which is reading data from SQL it doesn't work although when I call the file directly in the browser it returns valid JSON data... I even changed the ASP file to do nothing than only a
Response.Write
with hard coded JSON data - I also tried it with
Response.ContentType = "application/json"
, but this didn't help either - Then I loaded an own search.php file which again only does a
echo
of some hardcoded JSON data to https://softd4u.ch/search.php and tried to call this, but again without success
I found several samples in the Internet where the ASP file would be directly entered as source parameter (often older posts). I guess this might be working, but I think it makes sense to call the file via AJAX.
Trying to debug the jquery-1.12.4.js I at least found out that there is a 'parseerror' returned at one point, but I have no idea why and what I should do differently. At function ajaxHandleResponses( s, jqXHR, responses )
the variable responses was undefined in the case when it worked, but had content iin the case when it didn't work.
I'm sure the files are actually called (checked logs etc.) but they never trigger the success function of the AJAX call.
Does anyone have an idea what I'm missing here?
Source of classic asp file - version 1 (DB connection etc. handled in inc file):
<!-- #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("]")
%>
Source of classic asp file - version 2 (hardcoded response for test purpose; no need to retrieve querystring):
<%@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""}]")
%>