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

  • SEARCH
  • Home
  • 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 7004197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:10:04+00:00 2026-05-27T21:10:04+00:00

I have some code that requests some JSON from an API. When data is

  • 0

I have some code that requests some JSON from an API. When data is returned, per the documentation, it sends back a callback function that is to be used to parse the data at the top level. After the call is made, I have the following code to capture the data and process it:

var callback = 'functionUsedInApiCall';
window[callback] = newCallBackFunction;

How would I go about passing custom params to the callback function above as the data is being returned?

In order to capture the data, I must write the callback function like this:

function newCallBackFunction(root) {
   //root is the data
}

Any help would be greatly appreciated.

  • 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-27T21:10:05+00:00Added an answer on May 27, 2026 at 9:10 pm

    Are you talking about JSONP? If so, you don’t call the callback or pass in the argument at all, the code returned by the API does.

    E.g., your code:

    window.myCallback = newCallbackFunction;
    
    function newCallbackFunction(data) {
        // use the data
    }
    

    (I’m assuming this isn’t at global scope, hence assigning to the window object.)

    …plus your code for initiating the JSONP call, which is usually appending a script element to your page with a URL containing the name of the callback (“myCallback” in the above).

    Their response will look like this:

    myCallback({
        // data here
    });
    

    …which, when it arrives, will run (because it’s the content of a script element), and will call your function. This is how JSONP works.


    If you want to include further arguments for the function, all you do is have the callback they call turn around and call your target function, e.g.:

    window.myCallback = function(data) {
        newCallbackFunction(data, "foo", "bar");
    };
    
    function newCallbackFunction(data) {
        // use the data
    }
    

    Now when their code calls the global myCallback, all it does is turn around and call newCallbackFunction with that data and the arguments you specify.

    Those arguments don’t have to be literals as in the above. Here’s an example with a bit more context, using a closure:

    // Assume the url already contains the name of the callback "myCallback"
    function doJSONP(url, niftyInfo, moreNiftyInfo) {
         var script;
    
         // Set up the callback
         window.myCallback = function(data) {
            // Runs when the data arrives
            newCallbackFunction(data, niftyInfo, moreNiftyInfo);
         };
    
         // Trigger the request
         script = document.createElement('script');
         script.src = url;
         document.documentElement.appendChild(script);
    }
    

    Ideally, though, when doing JSONP you auto-generate the name of the callback each time so that it’s specific to the request (in case you have two outstanding requests at the same time):

    // Assume the url ends with "callback=" and we append the name of the
    // callback function to it
    function doJSONP(url, niftyInfo, moreNiftyInfo) {
         var cbname, script;
    
         // Get a callback name
         cbname = "callback_" +
                  new Date().getTime() +
                  "_" +
                  Math.floor(Math.random() * 10000);
    
         // Set up the callback
         window[cbname] = function(data) {
            // Remove us from the window object
            try {
                delete window[cbname];
            }
            catch (e) { // Handle IE bug (throws an error when you try to delete window properties)
                window[cbname] = undefined;
            }
    
            // Runs the function
            newCallbackFunction(data, niftyInfo, moreNiftyInfo);
         };
    
         // Trigger the request
         script = document.createElement('script');
         script.src = url + encodeURIComponent(cbname);
         document.documentElement.appendChild(script);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data Store that gets it's information from a JSON api on
I have some code in a javascript file that needs to send queries back
I have some code that gets the current logged in user. userID = request.session.get(_auth_user_id)
We have some legacy ASP.NET code that detects if a request is secure, and
I have some code that gives a user id to a utility that then
I have some code that uses the shared gateway pattern to implement an inversion
I have some code that raises PropertyChanged events and I would like to be
I have some code that looks like: template<unsigned int A, unsigned int B> int
I have some code that generates image of a pie chart. It's a general
I have some code that effectively does this : File file = new File(C:\\Program

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.