JQuery AutoComplete with Remote Data (COLDFUSION)

后端 未结 1 1348
轻奢々
轻奢々 2021-01-14 15:33

I\'m a newbie with JQuery trying to pull remote data from a ColdFusion query to show in my textbox.

I am receiving an error in firebug when calling my CFC:

相关标签:
1条回答
  • 2021-01-14 16:20

    (Expanded from comments...)

    As mentioned here, the error means the code was expecting a parsed JSON object, but was passed a simple string instead. jQuery is not parsing your cfc response string into a JSON object. Instead it is just passing the plain string into the response() function, which causes an error because that function expects an array. The reason the response is not parsed automatically is that datatype has the wrong case. It should be:

         dataType: "json"  // Note, upper case "T"
    

    As an aside, do not forget to var/local scope ALL of your function local variables, including query names.

    Update:

    I think a simpler approach here would be to change the cffunction argument name to term then use a URL as the source. Also, since you are not using any of the extra features of cfform, may as well switch to plain html elements instead.

    <script>
    $(document).ready(function() {  
             $( "#searchField" ).autocomplete({
                    source: "/kb/cfcs/search.cfc?method=lookupTitle&returnformat=json",
                    minLength: 3,
                    select: function(event, ui) {
                        $('#searchField').val(ui.item.value);
                        $('#defaultArticleID').val(ui.item.label);
                    }
                });
            });
    </script>  
    ...
    <form>
        <label for="searchField">Search field: </label>
        <input id="searchField">
        <input id="defaultArticleID">
    </form> 
    
    0 讨论(0)
提交回复
热议问题