2

Is it possible to use Ajax to POST data from within a Server-Side Javascript in the Marketing Cloud? Could the FormData be a problem?

I've been using this code:

var form = new FormData();
form.append("email", "[email protected]");
form.append("first name", "test");
form.append("last name", "test");
form.append("lists", "47");


var url = 'http://existing.working.website.com/';
var contentType = 'application/x-www-form-urlencoded';
var payload = form;
var headerNames = ["cache-control", "postman-token"];
var headerValues = ["no-cache", "a5f59d70-84dc-dce6-8809-03aa098cad3d"];
//var result = HTTP.Post(url, contentType, payload);
var result = HTTP.Post(url, contentType, payload, headerNames, headerValues);

Write("result: "+result.StatusCode + '<br>');
Write(result.Response);

and this one:

var form = new FormData();
form.append("email", "[email protected]");
form.append("first name", "test");
form.append("last name", "test");
form.append("lists", "47");


var settings = {
  "async": true,
  "crossDomain": true,
  "url": "http://existing.working.website.com/",
  "method": "POST",
  "headers": {
    "cache-control": "no-cache",
    "postman-token": "a5f59d70-84dc-dce6-8809-03aa098cad3d"
  },
  "processData": false,
  "contentType": false,
  "mimeType": "multipart/form-data",
  "data": form
}

$.ajax(settings).done(function (response) {
  console.log(response);
});

1 Answer 1

1

It should technically be possible, and I can't see anything off the top of my head that would cause a problem with your code, but I think it's useful to remind people who may read this that SSJS runs on page load and then anything else responsive or reactive would need to be client-side javascript. Same goes for ampscript.

Generally when you're using ajax you're working with the user post-page load to perform actions while they interact with the page, so you lose all the benefits of Ajax with SSJS as it can never engage with the user.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .