I have an Ajax request that sends some data to a page and expects back a truthy or falsey value depending on if the data was saved. In my controller I do everything and set the content to a true or false value. I really don’t want to create a view just to output 1 variable, so I was wondering if there was a way that I don’t have to use a view and only use the controller to output simple strings.
Share
I believe you cannot disable views completely, but there’s a pretty simple workaround: you can create one view and use it for many actions.
Let’s say we’ve created the view
views/main/ajax.cfm, what could be inside it? Obviously, simplest way is:Personally I like returning JSON, it allows me to have
statusfield, plus data, if needed. This way my view looks like this:Any way, now in our action we need to do something like this:
There’s one more gotcha for this. Sometimes you don’t want AJAX page to be accessed directly (like opened in browser), or vise-versa — want to do some debugging when it is.
There’s a cool helper
isAjaxin CFWheels framework, it is easy to port to the FW/1. It could be as simple as adding method like this to controller:Actually, that setup code above is also helper method in my apps:
So in my action code whole check looks like this, which is pretty compact and convenient:
Hope this helps.