Controller
setResponse()
Signature
Section titled “Signature”setResponse() — returns void
Available in: controller
Category: Rendering Functions
Description
Section titled “Description”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.
Parameters
Section titled “Parameters”| Name | Type | Required | Default | Description |
|---|---|---|---|---|
content | string | yes | — | The content to send to the client. |
Examples
Section titled “Examples”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);
}