I would like to POST to an SFMC SSJS Landing page using AJAX.
Is there any way to access the POST JSON Request Payload using SSJS in the Landing page?
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
Sign up to join this communityYou of course want to get the raw data of the POST, and parse it using ParseJSON function. The example below is what you want to do.
<script runat="server" language="javascript">
Platform.Load("Core","1");
var jsonpost = Platform.Request.GetPostData()
var json = Platform.Function.ParseJSON(jsonpost);
var content = json.element.child;
Variable.SetValue("@profile",content);
</script>
%%=v(@profile)=%%
To access the body of a request, you use the getPostData
method of the request object. This method takes as its parameter the number of characters of the body to return. If you specify 0, it returns the entire body. The return value is a string containing the requested characters. If there is no available data, the method returns the empty string. You can use this method to get all of the characters at once, or you can read chunks of data. Think of the body of the request as a stream of characters. As you read them, you can only go forward; you can’t read the same characters multiple times. To assign the entire request body to the postData variable, you can use the following statement:
postData = request.getPostData(0);
If you specify 0 as the parameter, the method gets the entire request. You can explicitly find out how many characters are in the information using the header’s content-length property, as follows:
length => parseInt(header["content-length"], 10);
To get the request body in smaller chunks, you can specify a different parameter. For example, the following code processes the request body in chunks of 20 characters:
var length = parseInt(header["content-length"], 10);
var i= 0;
while (i < length) {
postData = request.getPostData(20);
// ...process postData...
i = i +
20; }
Of course, this would be a sensible approach only if you knew that chunks consisting of 20 characters of information were meaningful in the request body.
I was able to solve this by Base64 encoding a stringified JSON object client side, and appending the resulting string as a query parameter value. (Note we have to URL encode any '=' or the parameter will be ignored by SFMC SSJS)
<script>
$.ajax({
url: landpageURL + "?data=" + btoa(JSON.stringify(jsonObject)).replace('=', '%3d'),
type: "POST",
success: function (response) {
// yay it worked
}
})
</script>
In SSJS we decode the string and parse the object;
<script runat="server">
// https://developer.salesforce.com/docs/atlas.en-us.noversion.mc-programmatic-content.meta/mc-programmatic-content/ssjs_syntaxGuide.htm
Platform.Load("core", "1.1.1");
var encodedString = Request.GetQueryStringParameter("data");
var decodedJsonString = Platform.Function.Base64Decode(encodedString);
var jsonObject = Platform.Function.ParseJSON(decodedJsonString);
Write(Stringify(jsonObject))
</script>