Salesforce-B2C-Commerce-Cloud-Developer Practice Test

Salesforce Spring 25 Release
202 Questions

A developer is asked to create a controller endpoint that will be used in a client-side AJAX request. Its purposes is to displayupdated information to the user when the request is completed, without otherwise modifying the appearance of the current page.
According to SFRA practices, which method best supports this objective?

A. res.json()

B. res.render()

C. res.print()

A.   res.json()

Explanation:

When handling client-side AJAX requests in Salesforce B2C Commerce SFRA, the best practice is to use:
res.json() — to send structured data (JSON) back to the browser for processing by client-side JavaScript.

📦 Why res.json() is best for AJAX:
It returns a JSON-formatted HTTP response, which is easy to handle in JavaScript.
It's ideal for partial page updates — such as cart updates, product info refresh, or stock availability.
It avoids re-rendering a full page, supporting a modern interactive UX.
✅ Example:
server.get('UpdateStock', function (req, res, next) {
var productInfo = {
stock: 12,
productId: '12345'
};
res.json(productInfo);
next();
});

🧠 In the browser:
fetch('/YourController-UpdateStock')
.then(res => res.json())
.then(data => {
document.querySelector('#stockCount').innerText = data.stock;
});

Why the other options are incorrect:

res.render()
Renders an ISML template (used for full pages or HTML fragments); not ideal for AJAX unless you're injecting raw HTML.
res.print()
Sends a raw string response; works, but lacks structure and makes parsing harder for the client. Not modern or clean.

📘 Reference:
SFRA Controller Response Methods
MDN – Fetch API

Salesforce-B2C-Commerce-Cloud-Developer Practice-Test - Home Previous
Page 23 out of 202 Pages