Skip to content

Controller

setResponse()

setResponse() — returns void

Available in: controller Category: Rendering Functions

Allows you to manually set the content that Wheels will send back to the client for a given request. Unlike renderView() or renderText(), which automatically generate output from templates or data, setResponse() gives you full control over the response content.

NameTypeRequiredDefaultDescription
contentstringyesThe content to send to the client.
1. Sending plain text
function myAction() {
    setResponse("This is a custom response sent directly to the client.");
}

2. Sending JSON content
function getUserData() {
    user = model("user").findByKey(1);
    
    // Convert the user object to JSON
    jsonData = serializeJson(user);
    
    // Set the JSON response
    setResponse(jsonData);
}
cfheader(name="Content-Type", value="application/json");

3. Sending HTML content
function showCustomHtml() {
    htmlContent = "<h1>Welcome!</h1><p>This is a custom HTML response.</p>";
    setResponse(htmlContent);
}