AJAX...CFCs, JSON and other geeky stuff

I have a customer that utilizes a third party web service for their end users to gather information. In this case, it was to get information on vehicles based upon their VIN number. I built a ColdFusion Component (CFC) to hit the web service, parse through the XML and pull back the appropriate data.

I then built a CFFORM to house the returned data in cfinput text fields. The goal was to save end users time by filling out the form for them. Within the form, I had a field where the end user could input the VIN. I had this working well with binding and all so that when the VIN was inserted all the fields were filled in...cool. 

But there was an issue. The end users were used to clicking a button to have the fields fill in and wanted to take that action. So I had to come up with a way to have the calls to the CFC take place upon clicking said button. Problem was that I was not quite sure how to do this. I have done stuff like that with straight CF and straight JS, but never the combo. 

So utilizing cfajaxproxy and JSON, I was able to get this to work. Allow me to show this.. (kudos to learncf that pointed me in the right direction).

First I set the AJAX stuff up by using the cfajaxproxy tag. This makes the connection to the cfc utilizing JSON.

<cfajaxproxy cfc="proxy" jsclassname="proxy" />

Then I wrote the JS script that hit the appropriate method to pull the info from the cfc.

 

function getYearvin(source) {
var instance = new proxy();
instance.setCallbackHandler(getYearvinSuccess);
instance.getYearvin(source);
}
 
If you notice, I used an item called 'setCallbackHandler' to pull back the result of the successful call to the method within the cfc.
 
I then wrote a function that sets the value of the cfinput text field to the returned value utilizing the DOM.
 
function getYearvinSuccess(result) {
document.getElementById('year').value = result;
}
 
Note that the function is what is called within the setCallbackHandler in the previous function.
 
I know...I know...really geeky and this may not get you as jazzed as it got me, but pretty cool, nonetheless. If anyone has any questions or better ways to handle this, let me know.
 

~Clay

Rate this

Comments

Post a Comment
  1. Leave this field empty

Required Field

Tag Cloud