[coldbox-3.6.0] Handling Ajax return values

Trying this for the first time and after a lot of reading, I’m in the home stretch.

So, in a form, a user clicks a checkbox as an on/off switch and a chunk of javascript is triggered that builds the url to then issue the following:

$.get(ajaxremote, function(data){
alert(data);
});

ajaxremote launches the following handler action:

function remoteChangeActive(event,rc,prc) {
var returnvalue = “false”;
var success = false;
event.showDebugPanel(false);
event.noRender();
if (StructKeyExists(rc,“id”) and rc.id != 0) {
success = actservice.updateActive(rc.id, rc.newvalue);
};
if (success) {returnvalue = “true”;};
//event.renderData(type=“html”,data=returnvalue);
return returnvalue;
};

I’ve tried several combinations and methods and while this works (in that a record in a database is properly updated) the returnvalue is always “” and I’m unsure why. Although its messy to debug, if I output returnvalue before the return in the event code, I can find that it is set to true, but no matter which way I handle the return of that data, it’s always “” on the calling end.

Maybe I just need to do this in a different way…most of the group talk is about returning HTML, but for me, I’m using ajax and CB events to manage on-the-fly record updates that just need to tell me if they were successful.

Approach alternatives or any suggestions welcome.

Mike

I have always used the returnData with no layout… Then have a struct that is return in json that either has a flag for success or fail and then get the JS to then decide what it needs to do. Obviously if it has a success then it has data as well.

that seems a much better way to handle simple return values…I have to think that either I’m doing something wrong the process (what comes first, what comes second) or something is wrong with my javascript. I think I changed the CF code the way you suggested:

handler code:

function remoteChangeActive(event,rc,prc) {
var returnvalue = {success=false};
event.showDebugPanel(false);
event.noRender();
if (StructKeyExists(rc,“id”) and rc.id != 0) {
returnvalue.success = actservice.updateActive(rc.id, rc.newvalue);
};
event.renderData(type=“json”,data=SerializeJSON(returnvalue));
//return returnvalue;
};

but I’m still getting “” as the return value in the javascript…I think maybe I need to learn a little bit more about ajax calls…but this was helpful Andrew, thanks.

Mike

event.renderData() works for me as well.

Example:

if( event.isAjax() ) {
var jsonData = {
data = [{ …somedata here… }],
success = true,
message = “I did something sweet!”
};
event.renderData( data=jsonData, type=“json” );
}

Remove the event.noRender() from your code

aaaah…structure the return value correctly…doh…I mis understood something Andrew said…let me try that.

event.noRender() : “This method tells the framework that this request will not produce any output, so just finalize the request. Most likely you will end up with a white page or if called from ajax, nothing.”

whoa…now that works, but the return value is still a string, giving me the struct Andrew was talking about, bu as a string

“{“SUCCESS”:true}”

so I can easily just build the string as “true” and test the data for that…but I like the struct approach and your (Exist~) structured return

Well, it’s a string, but it’s serialized JSON. So in jQuery, just decode it and you’ll have a JS object to work with

Incidentally, jQuery knows that if the response is served with a MIME type of application/json that it will automatically decode it for you and you don’t have to manually call $.parseJSON(myResponseString).