I'm trying to implement a jquery autocomplete control into our HTML page, but I'll be receiving my JSON from a URL resource.
Here's the code. If I set the source to the inline JSON
array (source: data
) then the autocomplete works well.
If I set the source to the WCF call (source: "http://MyServer:86/Service1.svc/GetStates"
), then the list is displayed when I enter any letter, but the filtering/autocomplete doesn't work.
Why isn't the autocomplete working when the JSON comes from a URL?
The json from the URL looks like this: [{"value":"AK","label":"Alaska"},{"value":"AL","label":"Alabama"}]
, exactly the same format as the inline json.
<!DOCTYPE html>
<html>
<head>
<title>jQuery UI Autocomplete: Using Label-Value Pairs</title>
<link href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/themes/ui-darkness/jquery-ui.min.css" rel="stylesheet">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/jquery-ui.min.js"></script>
</head>
<body>
<p>Default Behavior<br>
<input id="autocomplete1" type="text" placeholder="U.S. state name" name="code"></p>
<script>
var data = [
{ value: "AL", label: "Alabama" },
{ value: "AK", label: "Alaska" },
{ value: "AZ", label: "Arizona" },
{ value: "AR", label: "Arkansas" },
{ value: "CA", label: "California" }
];
$(function() {
$("#autocomplete1").autocomplete({
source: "http://MyServer:86/Service1.svc/GetStates",
dataType: 'json'
});
});
</script>
</body>
</html>