How to pass value from URL as an argument to ColdFusion function?

779 Views Asked by At

I am using JQuery and AJAX along with ColdFusion. I have some value in the URL http://mysitedomain.com/something/page.cfm?x=229 I want this value of x as an argument to my ColdFusion function.

Can somebody please tell me how can I pass value of x in URL as an argument to my ColdFusion function using AJAX or any other way?

I don't know how it works but still I tried doing this: AJAX code:

$.ajax({
    type: "GET",
    url: 'SomePathTocfc?method=getsomething&x='+x,
    async: true,
    success: function(result){
        document.getElementById('abc').value = "My Text Input";
    }
}); 

ColdFusion function:

<cffunction name="getsomething" access="remote" output="true">
   <cfargument name="x" type="string" required="true">
      <cfquery name="getReq" datasource="#application.dbsrc#" result =result>
          Select * from TableName 
          Where 
          <cfif  isDefined("arguments.x")  >
          x = #arguments.x#
          </cfif>
      </cfquery>
</cffunction>
1

There are 1 best solutions below

4
Jan On

just use <cfargument name="inparam" type="struct"> and you will get the url value as a structure and you can use it and do a <cfdump var="#arguments#"> and you will see your passed parameters, you can use them like this

<cffunction name="getsomething" access="remote" output="true">
   <cfargument name="inParam" type="struct" required="false">
      <cfquery name="getReq" datasource="#application.dbsrc#" result =result>
          Select * from TableName 
          Where 1=1 
<cfif inParam.keyExists('x')>
         and x = <cfqueryparam sqltype="integer" value="#inparam.x#">
</cfif>
          
      </cfquery>
</cffunction>