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 7593911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:17:20+00:00 2026-05-30T21:17:20+00:00

For a project of mine I need to do multiple calls to a (remote)

  • 0

For a project of mine I need to do multiple calls to a (remote) API using JSONP for processing the API response. All calls use the same callback function. All the calls are generated dynamically on the client’s side using JavaScript.

The problem is as follows: How do I pass additional parameters to that callback function in order to tell the function about the request parameters I used. So, e.g., in the following example, I need the myCallback function to know about id=123.

<script src="http://remote.host.com/api?id=123&jsonp=myCallback"></script>

Is there any way to achieve this without having to create a separate callback function for each of my calls? A vanilla JavaScript solution is preferred.

EDIT:

After the first comments and answers the following points came up:

  • I do not have any control over the remote server. So adding the parameter to the response is not an option.
  • I fire up multiple request concurrently, so any variable to store my parameters does not solve the problem.
  • I know, that I can create multiple callbacks on the fly and assign them. But the question is, whether I can avoid this somehow. This would be my fallback plan, if no other solutions pop up.
  • 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-30T21:17:22+00:00Added an answer on May 30, 2026 at 9:17 pm

    Your options are as follows:

    1. Have the server put the ID into the response. This is the cleanest, but often you cannot change the server code.
    2. If you can guarantee that there is never more than one JSONP call involving the ID inflight at once, then you can just stuff the ID value into a global variable and when the callback is called, fetch the id value from the global variable. This is simple, but brittle because if there are every more than one JSONP call involving the ID in process at the same time, they will step on each other and something will not work.
    3. Generate a unique function name for each JSONP call and use a function closure associated with that function name to connect the id to the callback.

    Here’s an example of the third option.

    You can use a closure to keep track of the variable for you, but since you can have multiple JSON calls in flight at the same time, you have to use a dynamically generated globally accessible function name that is unique for each successive JSONP call. It can work like this:

    Suppose your function that generate the tag for the JSONP is something like this (you substitute whatever you’re using now):

    function doJSONP(url, callbackFuncName) {
       var fullURL = url + "&" + callbackFuncName;
       // generate the script tag here
    }
    

    Then, you could have another function outside of it that does this:

    // global var
    var jsonpCallbacks = {cntr: 0};
    
    
    function getDataForId(url, id, fn) {
        // create a globally unique function name
        var name = "fn" + jsonpCallbacks.cntr++;
    
        // put that function in a globally accessible place for JSONP to call
        jsonpCallbacks[name] = function() {
            // upon success, remove the name
            delete jsonpCallbacks[name];
            // now call the desired callback internally and pass it the id
            var args = Array.prototype.slice.call(arguments);
            args.unshift(id);
            fn.apply(this, args);
        }
    
        doJSONP(url, "jsonpCallbacks." + name);
    }
    

    Your main code would call getDataForId() and the callback passed to it would be passed the id value like this followed by whatever other arguments the JSONP had on the function:

    getDataForId(123, "http://remote.host.com/api?id=123", function(id, /* other args here*/) {
        // you can process the returned data here with id available as the argument
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a linguistic project of mine I need to use symbols not contained in
Developing a project of mine I realize I have a need for some level
In a small project of mine I've came across the need of a collapsible
As part of a pet project of mine, I need to test the performance
I need to import the RSyntaxTextArea to a project of mine, but i can't
For a project of mine I need to detect when the user blows into
i need some help with a project of mine. It is about a dvd
For a project of mine, I need two properties to be set at the
I am using the Nivoslider script for a project of mine. Basically I'm needing
For a certain Perl project of mine I need several Perl processes to share

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.