Ajax call in a module

I am trying to figure out how to make an ajax call in a module and I am not even sure where to begin. If I am in a module can I even use the coldbox proxy or is that out the window? I don’t think you would want the user to have to configure that in the parent application.

So I made a function in my users handler and if you go to the url below in a browser window you can hit it.

/**

  • @returnformat json
    */
    remote string function usernameExists(event){
    var rc = event.getCollection();
    var user = userService.findWhere({username=rc.username});
    return !isNull(user);
    }

http://localhost/coldboxcatalog/security/users/usernameExists/admin = returns yes

but if I try and just hit it through an ajax request

$(’##username’).keyup(function(){

// cache this
var t = this;

if(this.value != this.lastValue) {

if(this.timer){
clearTimeout(this.time);
}

$validateUsername.removeClass(‘error’).html(‘checking availibility …’);

this.timer = setTimeout(function(){
$.ajax({
url: ‘http://localhost/coldboxcatalog/security/users/usernameExists/’,
data: t.value,
type: ‘post’,
dataType: ‘json’,
success: function(data){
$validateUsername.html(data.msg);
}
})
}, 200);

this.lastValue = this.value
}
});

I get a 500 (Internal Server Error)

I personally still run my requests through a coldbox proxy object even in modules.
<cfcomponent name="SomeProxy" output="false" extends="coldbox.system.remote.ColdboxProxy" hint="Proxy to tie our ajax calls to coldbox">

<cffunction name="someFunction" access="remote" returntype="any" output="false" returnformat="json" hint="">

<cfscript>

arguments.event = "moduleName:handler.eventName";

results = super.process(argumentCollection=arguments);

return results;

</cfscript>

</cffunction>

</cfcomponent>

Then my ajax call might look like

$.post('moduleName/remote/SomeProxy.cfc?method=someFunction', {

ID: row.id,

newIndex: newIndex,

oldIndex: oldIndex

}, function(result){

alert("Called Proxy");

});

Curt Gratz

The URL call would be better suited to whatever your URL is, so if it is SES then

http://localhost/security/users/usernameExists/

Security = Module

Users = handler

usernameExists = action

This is how I do all my Ajax calls, via the handler using RESTFul calls.

Regards,

Andrew Scott

http://www.andyscott.id.au/

@Curt…awesome, I will give that a try…thank you :slight_smile:

@Andy localhost/coldbox/ is my base url …

Curt…that works pretty well if its in the root of my project but In the route of my module I am having issues. The main route for the module is

{pattern="/", handler=“security”,action=“index”},

So I can figure out how would I ever hit the cfc through a url…

sorry for the dumb questions :frowning:

Why don’t you use the standard module:handler.action in a RESTFul way?

I am not sure you got what I was trying to say to you before, with modules I have found this the most effective way to use Ajax.

Regards,

Andrew Scott

http://www.andyscott.id.au/

there is no handler or action in this case is there ?

module:handler.action

/solitary
-Application.cfc
-index.cfm
-ColdboxProxy.cfc

calling soltiary: is not a valid route…

Ok, I don’t use ColdboxProxy. I think this is more for those who are doing Flash and that type calls, I prefer to use a normal controller/handler for all my Ajax calls.

So what I am saying is that instead of calling it through the proxy like you are trying, just do a normal event handler call through you Ajax URL call.

And if you use the RESTFul features in the handler you can lock it down to just a get call, etc.

Hope that makes a bit more sense.

Regards,

Andrew Scott

http://www.andyscott.id.au/

Andy…

So your suggesting just hitting the handler

http://localhost/coldboxcatalog/security/users/checkUsername/admin

in my event handler how do I return the result to the ajax method? I tried just return result but that didn’t work. Do you have an example of this ?

Figured out my issues…thanks Andy…

I was trying to return the data right from the method which was causing the layout to load and in turn debugging was loading so the xhr call was messed up…

here is my handler method (don’t get caught up on the result, I was just playing around here…

public void function usernameExists(event) {
var rc = event.getCollection();
var user = userService.findWhere({username=rc.username});
var result = {};

if( isNull(user) ){
result[‘exists’] = false;
result[‘msg’] = “username is available”;
} else {
result[‘exists’] = true;
result[‘msg’] = “username is unavailable”;
}

event.renderData(type=“JSON”,data=result,nolayout=true);
}

and my script

Yes, that is what I was going to write. Leverage renderData() to return marshalled data and just use your normal ses handling for this. I personally don’t use the proxy unless I am doing Air or Flex or cfc data binding.
Luis F. Majano
President
Ortus Solutions, Corp

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com