Pull Down with binding and AJAX

后端 未结 2 457
轮回少年
轮回少年 2021-01-20 12:02

I am new to ColdFusion and just started learning about Ajax. The question I have is that I found on the web some cool coding to implement two pull down menus, where is the s

相关标签:
2条回答
  • 2021-01-20 12:41

    Testing CFC's

    Always test cfc's in CF before plugging them into ajax. There is no point messing with bindings until after you have verified the cfc works without error, because if it does not work in CF, it is not going work with ajax either. The only difference is the errors will be harder to find.

    As others suggested, start small. Test the query by itself. Then move onto to testing the CFC either using <cfinvoke> or simply invoke it from your browser with test values like:

    http://yourserver/path/to/select.cfc?method=getServiceType2&cat_desc= 
    http://yourserver/path/to/select.cfc?method=getServiceType2&cat_desc=someValue
    

    Error

    In regards to the error, we need to see the full error message to provide more specific advice. However, looking at the query/code some likely causes are:

    1. CAT_DESC is a varchar column, in which case your argument must be enclosed in single quotes. Otherwise the database will think the argument value is an object name (table or column).

      WHERE CAT_DESC = '#ARGUMENTS.CAT_DESC#' 
      
    2. .. OR CAT_DESC is a numeric column, but your argument is empty. That would result in an invalid sql statement. You need to ensure a valid number is passed into the query (Or you could skip the WHERE clause when the argument is empty, depending on the desired results). One common approach is using the val() function to convert empty strings and other non-numeric values to zero, ie:

      WHERE CAT_DESC = #val(ARGUMENTS.CAT_DESC)#

    3. It also looks like you have a stray </cfif> after the second query. (Assuming it is not a copy/paste error)

    4. One other thing, your second query specifies both datasource and dbtype. Those two attributes are mutually exclusive. Having both may cause a syntax error (I have not tried it). Either way you should only use one of them (most likely datasource).

    SQL Injection

    That said - the query above is vulnerable to sql injection. You should always use <cfqueryparam> on all variable query parameters to guard against sql injection. It has other benefits as well (performance, data type checking, etcetera). But sql injection protection is the most critical in a web application.

    <!--- substitute the correct type for your db column --->
    WHERE CAT_DESC = <cfqueryparam value="#ARGUMENTS.CAT_DESC#" 
                             cfsqltype="cf_sql_varchar">
    

    Improvements

    As of CF8.0.1+, cfselect can bind to a query object (not just an array). So instead of building an array, simply return the raw query from the function.

        <cffunction name="getServiceType2" access="remote" returnType="query">
            <cfargument name="CAT_DESC" ....>
    
            <cfset var data="">
    
            <cfquery name="data" ..> 
               SELECT TheQueryColumnToDisplay, TheQueryColumnUsedForListValue
               FROM  ... 
               WHERE ... 
            </cfquery>
    
           <cfreturn data>
        </cffunction>
    

    Then specify which column(s) to use for the display and value attributes:

        <cfselect name="service" 
                  bind="cfc:select.getServiceType2({catdesc})"
                  display="TheQueryColumnToDisplay"
                  value="TheQueryColumnUsedForListValue" ...>
    
    0 讨论(0)
  • 2021-01-20 12:48

    My suggestion is to do one thing at a time. For your specific situation,

    First, get your query to work with a cfquery tag.

    Second, get it to work inside a function where you pass an argument to the function.

    Next, put the function inside a cfc and call it from a cfc page using either or by creating an object and calling the function.

    Finally do the bind.

    This approach will make the errors more visible so that you can do something about them.

    Other observations

    1. use query parameters.
    2. try to pass an id field to your query instead of a text description
    0 讨论(0)
提交回复
热议问题