How to pass CFC argument through AJAX?

I use the following script to call the CFC function:

function loadQuery() { $.get('QueryData.cfc',{},function(GetMyData){ $("#content").html(GetMyData) }) return false } $(document).ready(function() { $("#loadLink").click(loadQuery) }); 

This is my HTML:

 <a href="" id="loadLink">Load It</a> <div id="content"></div> 

I call the following CFC:

 <cffunction name="GetMyData" access="public" returntype="query"> <cfargument name="RecordID" type="string" required="yes"> <cfset var RecordData = ""> <cfquery name="RecordData" datasource="MyDSN"> SELECT foo.RecordID, foo.RecordName FROM foo WHERE foo.RecordID = #ARGUMENTS.RecordID# ; </cfquery> <cfreturn RecordData> 

The problem is that when I call CFC, the CFC page appears; a CFC description appears (after asking for the administrator password). I do not want to load QueryData.cfc; I want to execute a function inside QueryData.cfc.

The second problem is that I cannot understand the syntax for passing an argument to the CFC method.

+4
source share
2 answers

You can do something similar with the $ .get method, but usually I do something like this:

 $(document).ready(function() { $("#loadLink").click(function(e) { e.preventDefault(); var recordata = $(this).attr("href").substring(1); //trim '?' char $.ajax({ type: "GET", url: "QueryData.cfc?method=GetMyData", data: recordata, dataType: "html", success: function(message) { $("#content").html(message); } }); }); }); 

If the data for the record ID is stored somewhere in the DOM, for example:

 <a href="?RecordID=#url.RecordID#" id="loadLink">Load Data</a> <div id="content"></div> 

Also, I’m not sure how it behaves with access = "public" - it can still work, but probably it should have access = "remote" to your function.

+5
source

Why do you want to try <cfdiv> or <cfajaxproxy> ? It is much simpler.

But to answer your question, the GET url must be XXX.cfc?method=whatever¶m=xyz

edit: btw your function should have access="remote" , and it is not recommended that you return a Query object if you are not using <cfgrid> .

+1
source

Source: https://habr.com/ru/post/1303143/


All Articles