Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Note
The Retail Interest Group by Dynamics 365 Commerce has moved from Yammer to Viva Engage. If you don't have access to the new Viva Engage community, fill out this form (https://aka.ms/JoinD365commerceVivaEngageCommunity) to be added and stay engaged in the latest discussions.
This article describes how to call server-side data actions by using Asynchronous JavaScript and XML (AJAX) in Microsoft Dynamics 365 Commerce.
The Dynamics 365 Commerce online software development kit (SDK) supports using AJAX to invoke server-side data actions from the client browser. Use this feature in scenarios where data actions contain sensitive information, such as an API key or secret that is required to call a partner service. For these scenarios, don't reveal sensitive information through a client-side data action call.
To use AJAX to invoke a data action on the server, use the following URL route. The ID parameter value specifies the data action that runs on the server. This URL route supports both HTTP GET and POST requests.
/api?id=<data_action_id>
The online SDK provides the commerceAPIRequest helper utility function. This function provides controls for AJAX requests, such as the ability to support more context information.
GET request format
import * as MsDyn365 from '@msdyn365-commerce/core';
public async componentDidMount(): Promise<void> {
const response = await MsDyn365.commerceApiRequest(
this.props.context.request,
'<data_action_id>',
'get'
);
}
POST request format
import * as MsDyn365 from '@msdyn365-commerce/core';
public async componentDidMount(): Promise<void> {
const response = await commerceApiRequest(
this.props.context.request,
'<data_action_id>',
'post',
{
content: 'sample text'
}
);
}
For POST requests, access the post body from the ctx.requestContext.postBody context object inside the data action, as shown in the following example.
async function action(
input: Msdyn365.IActionInput[],
ctx: Msdyn365.IActionContext
): Promise<IWeatherConditions[]> {
const postBody = ctx.requestContext.postBody?.content;
return postBody;
}