Is there a way to transfer a JSON array from a text/javascript block to a runat=server block within the context of ExactTarget/Salesforce Marketing Cloud?

I have a page containing two <script> elements: one is running locally and the other one is running on the ExactTarget server:

<script type="text/javascript">
var results = localStorage.getItem('results');
var results = JSON.parse(results);
</script>

<script runat=server>
Platform.Load("Core","1");
Write(Stringify(results)); //currently returns a 'null' value
var campaignsupdate = DataExtension.Init("0DB9904F-CE05-45E7-8052-    7C6935B4B012");
for(var i=0; i < results.length; i++) {
var rowid = results[i]["ROWID"];
var title = results[i]["title"];
campaignsupdate.Rows.Update({title: title},["ROWID"], [rowid]); 
}
</script>

Right now, when 'results' is called in the 'runat=server' block, it returns null. How can I access the 'results' array in the 'runat=server' block?

Answer №1

Unfortunately, it is not possible to access the variable 'results' in Server Side Java Script (SSJS) as it runs on the server before the client-side JavaScript is executed. There is no direct communication between SSJS and JavaScript, so using JavaScript libraries or code within an SSJS block is not viable.

One workaround is to pass a value in the GET/POST request and retrieve it in SSJS. For more information, you can refer to the core library server side JavaScript functions documentation in the Request Object and Functions area.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Assign the variable of one function to another function

I have a collection of buttons with usernames as values (such as jason, chan, brad, etc.). When a user clicks on a button, it is moved to a specific div. For example: <input type="button" onClick="nano();" id="name1" class="names" name="jason" value=" ...

Run a function continuously while a key is being held down

I am currently working on a Javascript program that will move a div up and down based on key inputs. The idea is to continuously update the 'top' style value of the div while a specific key is pressed. While the basic function works, I am struggl ...

Async/Await moves on to the next function without waiting for the previous function to finish executing

I am developing a web application that requires querying my database multiple times. Each query depends on the data retrieved from the previous one, so I need to ensure each call completes before moving on to the next. I have attempted using async/await fo ...

"Utilizing the 'await' keyword within a JavaScript 'for of'

Could there be an issue with my code? I am utilizing express and mongoose router.get('/c/:hashtoken', validateEmailToken, catchAsync(async(req,res)=>{ const hashtoken = req.params.hashtoken const hashtoken2 = createHash('sha256&ap ...

The loop will simply continue if it does not proceed past the `else` keyword

I am struggling to implement JSON data for the first time in my project. I want to display this data in a UIView with some labels, but I am facing difficulties. When I add breakpoints after `else { continue }`, the debugger skips that part and moves on wit ...

What is the best way to separate a JSON response into different sections based on the value of the name field?

Hello, I am currently making an API call to the Myfxbook API and receiving a JSON response with a large amount of data. Is it possible to break down this extensive response into multiple smaller parts based on the 'name' value? Here is a snippet ...

How to refine a schema by applying filters from another schema

I am working with 2 different Schemas const mongoose = require('mongoose'); const Schema = mongoose.Schema; const {ObjectId} = mongoose.Schema; const matchList = new Schema({ user:[{ type: ObjectId, ref:'User' } ...

React's componentDidUpdate being triggered before prop change occurs

I am working with the CryptoHistoricGraph component in my app.js file. I have passed this.state.coinPrices as a prop for this element. import React from 'react'; import axios from 'axios'; import CryptoSelect from './components/cry ...

Incorporating a requirement into a function to ensure it is executed only under certain circumstances

I'm currently developing a text-based game using JavaScript that involves picking up a sword with the help of an in-game function. var takeSword = function() { if (currentRoom.hasSword) { $("<p>You picked up a sword.</p&g ...

What are some ways to utilize symbol fonts such as marvosym within a web browser?

Are you looking to use special LaTeX fonts like \Pluto from marvosym in your web development projects? Wondering how to display this symbol with HTML / JavaScript / CSS without relying on an image? You can download the font from This symbol corresp ...

How can I adjust the border width of outlined buttons in material-ui?

Currently, I am in the process of modifying a user interface that utilizes Material-UI components. The task at hand involves increasing the thickness of the borders on certain outlined buttons. Is there a method through component props, themes, or styles t ...

Pass a JavaScript array variable to a PHP script utilizing jQuery Ajax and receive a string output instead of an array

Whenever I attempt to transmit a JavaScript array to a PHP script via Ajax, the output of $_POST['file_paths'] when var_dumped shows up as a string instead of an array. My goal is to have the output in array format rather than in string format. C ...

A guide on displaying containers with jQuery and CSS

Looking to create a smiley survey using only Front-End technologies. Once a radio button is selected, the associated content should appear for comments. Currently, I have set up my CSS with display: none. I attempted to implement this functionality using ...

Attempting to generate a cost estimator, however, no results are showing up upon clicking the calculate button

Having based my code on a template and being fairly new to Javascript, I expected everything to work smoothly. However, upon testing it, I encountered an issue where nothing was displayed in the results boxes. My goal is to develop a pricing calculator th ...

I encountered a crash in my app because of an error in my Node.js backend code that was posting the accessories and slug into the database

My node.js backend code is responsible for adding the accessory and slug to the database, but I am encountering app crashes. const express=require('express'); const Category = require("../models/Category"); const slugify=require('s ...

When the key property is utilized, the state in react useState is automatically updated; however, for updating without it, the useEffect or a

I am working on a component that looks like this: import React, { useState } from "react"; import { FormControl, TextField } from "@material-ui/core"; interface IProps { text?: string; id: number; onValueChange: (text: stri ...

How can I reference a function in a single file component using Vue.js?

Within my Vue.js project, I have crafted a single file component known as Password.vue which comprises two password fields along with their associated validation checks. To begin with, I structure my HTML within the <template></template> tags, ...

Refreshin the attached DOM to a directive without a page reload

Within a directive, I have implemented some text and a video tag in the code below: app.directive('ngAzuremediaplayer', function () { return { restrict: 'AE', priority: 10, link: function (scope, elem, attr ...

Transitioning from the older version of Angular (1.0.8) to the newer version (1.2.0

Transitioning to the latest production version of Angular has been quite challenging for me. I have meticulously followed the migration guidelines, focusing mainly on the underscore prefix \ private attributes section that seemed relevant to my situa ...

Limit the number of ajax requests that can be initiated to once, similar to a button click

I have a like button that, when clicked, increments a value in my database table by 1. Upon clicking the button: var button_id = $(e.target).closest('div').attr('id'); var id = button_id.substring(12); $.ajax({ method: &apos ...