0

I have a code on client-side that is using JavaScript to create an object called "data" that contains several variables such as "matricula", "scheduled", "selectedDate", "selectedChoices", "workshop", and "daySlot". The object is then being converted to a JSON string and sent to a specific URL using an XMLHttpRequest with a "POST" method. Then I have a landing page that is using AMPscript to retrieve the values of the variables that were sent in the previous code using the RequestParameter function. Then it's using the InsertDE function to insert the values into a Data Extension.

The problem is, when I test the code, it is adding an empty row to the Data Extension. Can anyone help me too see what could be the problem here? Thank you!

Client-Side JS code:

const sendData = function() {

      var data = {
         "matricula": matricula,
         "scheduled": scheduled,
         "selectedDate": selectedDate,
         "daySlot": daySlot,
         "selectedChoices": selectedChoices,
         "workshop": workshopSelected
      };

      var xhr = new XMLHttpRequest();
      xhr.open("POST", "https://mcwjw68xg1lkqk0bj841tnslyx5y.pub.sfmc-content.com/r5g5qsdldwq");
      xhr.setRequestHeader("Content-Type", "application/json");
      xhr.send(JSON.stringify(data));

      console.log(JSON.stringify(data));
};

AMPscript code:

%%[
  var @insertCount

  SET @matricula = RequestParameter("matricula");
  SET @selectedDate = RequestParameter("selectedDate");
  SET @selectedChoices = RequestParameter("selectedChoices");
  SET @workshop = RequestParameter("workshop");
  SET @daySlot = RequestParameter("daySlot");
  SET @scheduled = RequestParameter("scheduled");
  
  InsertDE("Proactive Maintenance Data", "Matrícula", @matricula, "Agendou", @scheduled, "Data Escolhida", @selectedDate, "Altura do dia", @daySlot, "Razões", @selectedChoices, "Confessionário", @workshop);

]%%
1
  • 2
    RequestParameter is used for querying values from URL query strings, and you’re posting a json, I don’t think it’s going to work. Either post the data in a query string, or use SSJS in the second page to retrieve the data, parse it and insert into a DE. Additionally, the InsertDE function is meant for emails - use InsertData on landing pages instead
    – zuzannamj
    Commented Jan 24, 2023 at 18:32

1 Answer 1

2

The reason it is inserting blank is that the Parameter you are looking for to fill your AMPScript variable does not exist.

RequestParameter looks for a query string on the URL (usually added for GET methods) or a form field passed in a POST and will then pull the value of the matching 'key' that you entered. For example:

URL: https://www.google.com?myVar=Value
AMPscript: %%[ SET @myVar = RequestParameter("myVar") ]%%
Output: Value

As you are POSTing the data and using the JSON you created as the payload of the POST, your RequestParameter will never see it and will never be able to retrieve it.

What you need to use instead is GetPostData. This will retrieve the JSON that is the POST data (aka payload) and store it in a SSJS variable.

You can then at that point would need to turn the string JSON into a JSON object. From there you can then pull out the parts you need utilizing dot and number indexes accordingly. Then you would just do your Insert call via SSJS instead. For example:

<script runat=server>
Platform.Load("Core","1.1.1");

var myJSONStr = Platform.Request.GetPostData();
var myJSON = Platform.Function.ParseJSON(myJSONStr);

var marticula = myJSON.marticula;
var selectedDate = myJSON.selectedDate
var ......

  var rows = Platform.Function.InsertDE("Proactive Maintenance Data",["Marticula","Agendou","Data Escolhida","...etc..."],[marticula, scheduled, selectedDate, ...etc...]);

Otherwise you could just POST it using form data as the content type and then just parse out the JSON to pass individual values in as form fields and you can then use RequestParameter on your endpoint to get those.

1
  • 1
    Thank you for the complete explanation! It is working now! But, as @zuzannamj said I had to use InsertData instead of InsertDE
    – Raas
    Commented Jan 25, 2023 at 16:26

You must log in to answer this question.

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