Using Ajax With Coldbox

Hi there

I am trying to create an ajax function in my coldbox application,
following the Task Manager demo, but it doesnt seem to be functioning
for me..

Initially I am not sure whether I can call the function from my
external JS file? Because in the example the JS is hardcoded on the
page and uses cfsavecontent..

Regardless I have the following code in my external js file

  function saveTask(){
    $(’.saveTask’).click(function(){
      // Hardcoded variables just as an example..
      var sTitle = ‘hello’;
      var iTaskListID = 1;
      $.post(‘http://localhost:8501/toDoLists/index.cfm/tasks/save’,
{sTitle:sTitle,iTaskListID:iTaskListID},function(data){
      // Processing goes here
      },“json”)
    });
  }

And my tasks/save handler is based on the one in the Task Manager Demo

     var rc = event.getCollection();

      rc.task = taskService.getTask(event.getValue("taskID",""));
      populateModel(rc.task);
      taskService.save(rc.task);

This doesnt appear to be working for me, could anyone point me in the
right direction?

Many thanks

Hi.

I don’t see anything in your handler event that is actually returning any kind of data. Your JS function appears to be expecting JSON, but in the handler the last executable line simply calls a method on a service layer object. Unless I’m missing something, I think the final piece of the puzzle you’re missing to bridge this gap is probably the event.RenderData() method, which is used almost exclusively for the purpose of creating handler methods intended to be consumed by remote/ajax calls.

Since the method you’re calling is simply a save method, I’m not sure what data you’d be returning, except perhaps a status flag indicating success or failure. But, whatEVER you wanted returned, you’d do it with something like the following in your handler method:


status = taskService.save(rc.task);
event.RenderData(type=“JSON”,data=status);

Doug

Hi there

Thanks for the response..

Looks like im going about this the wrong way...

For the moment I dont really need to return any data, im just looking
to save info into the database..

However when I click on my link that calls the "save task" function it
doesnt work.....the post shows an error in firebug...it has no
specific details, it just flags it up in red.

I usually post directly to the method itself, like so

  function saveTask(){
    $('.saveTask').click(function(){
      var sTitle = 'hello';
      var iTaskListID = 1;
      $.post('http://localhost:8501/toDoLists/model/taskService.cfc?
method=save',{sTitle:sTitle,iTaskListID:iTaskListID},function(data){

      })
    });
  }

Which works, but I dont want to bypass the coldbox framework to
achieve what im doing.

I have tried add renderData, but its still bringing up an error in
firebug...

Thanks