Back

setResponse()

Name Type Required Default Description
content string Yes The 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);
}
Copied!