Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6349527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:38:02+00:00 2026-05-24T21:38:02+00:00

I have a server side Python script that returns a JSON string containing parameters

  • 0

I have a server side Python script that returns a JSON string containing parameters for a client side JavaScript.

# Python
import simplejson as json

def server_script()
  params = {'formatting_function': 'foobarfun'}
  return json.dumps(params)

This foobarfun should refer to a JavaScript function. Here is my main client side script

// JavaScript
function client_script() {
  var xhr = new XMLHttpRequest();
  xhr.open("GET", url, async=true);
  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
      options = JSON.parse(xhr.responseText);
      options.formatting_function();
    }
  };
  xhr.send(null);
}

function foobarfun() {
  //do_something_funny_here...
}

Of course, options.formatting_function() will complain that “a string is not callable” or something to that effect.

Upon using Chrome’s Inspect Element, under the Resources tab, and navigating the left sidebar for XHR > query, I find that client_script interprets options as below. foobarfun is seen as a string.

// JavaScript
options = {"formatting_function": "foobarfun"}

I would have liked client_script to see options as

// JavaScript
options = {"formatting function": foobarfun}

Of course, doing the following within Python will have it complaining that it doesn’t know anything about foobarfun

# Python
params = {'formatting_function': foobarfun}

QUESTION:
How should I prepare my JSON string from the server side so that the client script can interpret it correctly? In this case, I want foobarfun to be interpreted as a function object, not as a string.

Or maybe it’s something I should do on the client side?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:38:03+00:00Added an answer on May 24, 2026 at 9:38 pm

    There’s nothing you can do in the JSON to get the result you want because JSON has no concept of functions, it’s purely a data notation. But there are things you can do client-side.

    If your foobarfun function is a global function (which I would recommend against, we’ll come to that), then you can call it like this:

    window[options.formatting_function]();
    

    That works because global functions are properties of the window object, and you can access properties either by using dotted notation and literals (window.foobarfun), or by using bracketed notation and strings (window["foobarfun"]). In the latter case, of course, the string doesn’t have to be a string literal, it can be a string from a property — your options.formatting_function property, for instance.

    But I don’t recommend using global functions, the window object is already very crowded. Instead, I keep all of my functions (or as many as possible, in some edge cases) within a master scoping function so I don’t add anything to the global namespace:

    (function() {
        function foobarfun() {
        }
    })();
    

    Now, if you do that, you can’t access foobarfun on window because the whole point of doing it is to avoid having it be on window. Instead, you can create your own object and make it a property of that:

    (function() {
        var myStuff = {};
    
        myStuff.foobarfun = foobarfun;
        function foobarfun() {
        }
    
        function client_script() {
          var xhr = new XMLHttpRequest();
          xhr.open("GET", url, async=true);
          xhr.onreadystatechange = function() {
            if (xhr.readyState == 4) {
              options = JSON.parse(xhr.responseText);
              myStuff[options.formatting_function]();   // <== using it
            }
          };
          xhr.send(null);
        }
    })();
    

    Frequently, rather than this:

    myStuff.foobarfun = foobarfun;
    function foobarfun() {
    }
    

    you’ll see people write:

    myStuff.foobarfun = function() {
    };
    

    I don’t recommend that, either, because then your function is anonymous (the property on myStuff that refers to the function has a name, but the function doesn’t). Giving your functions names is a good thing, it helps your tools help you (showing you the names in call stacks, error messages, etc.).

    You might also see:

    myStuff.foobarfun = function foobarfun() {
    };
    

    and that should be valid, it’s correct JavaScript. But unfortunately, various JavaScript implementations have various bugs around that (which is called a named function expression), most especially Internet Explorer prior to IE9, which will create two completely different functions at two different times.

    All of that said, passing the names of functions around between the client and server usually suggests that you want to step back and look at the design again. Data should drive logic, but not in quite such a literal way. That said, though, there are definitely valid use cases for doing this, you may well have one in your situation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to maintain a server-side script written in JScript (NOT Javascript) that needs
Python 2.7, Windows XP. I have a server that sends messages to client(s). I
I have some python code for Google App Engine that responds with the string
Is it possible to have a server side program that queues and manages processes
I have a Java server that will accept a connection from a python socket
I have started to write a Python 3.x client application. The server application exists
I have an isolated python script that simply captures data from Twitter's streaming API
I have a server side ImageButton, and a server side Button, and then a
I'm trying to adress the following issue: I have a server side .net application
What tools for server side application performance testing you suggest? Have an application server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.