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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:49:02+00:00 2026-05-15T01:49:02+00:00

This whole jsonp thing is quite confusing… Here is what I want to do:

  • 0

This whole jsonp thing is quite confusing…

Here is what I want to do:

  • I have a class DataRetriever
  • The class has a method GetData
  • GetData makes a jsonp request with the following code:

    var new_tag = document.createElement('script');
    new_tag.type = 'text/javascript';
    new_tag.src = 'http://somesite.com/somemethod?somedata';
    // Add the element
    var bodyRef = document.getElementsByTagName("body").item(0);
    bodyRef.appendChild(new_tag);
    

Now, the jsonp data from the server somesite.com can call a function in my code with the data. The problem is, how does the data get delivered to the instance of DataRetriever that requested it?

I’m really stuck here.

  • 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-15T01:49:02+00:00Added an answer on May 15, 2026 at 1:49 am

    The solution jQuery came up with, is to provide an anonymous callback function like this:

    jQuery.getJSON("http://mycrossdomain.com/?callback=?", function(data) {
       // Now I have the data
    });
    

    I think this could be adapted to your case as well.

    var data = new DataRetriever();
    data.GetData(function(data) {
        // Now I have the data 
    });
    

    You could do the same thing behind the scenes in the GetData function if you didn’t want to provide an anonymous function.

    function GetData(callback) { // optional
        // Create random function name however you want
        var funcName = "data_" + (+new Date() + Math.floor(Math.random()*100)),
            // Moved this up to be available in the random function
            new_tag = document.createElement('script');
        // This part will allow you to do the callback behind the scenes if callback isn't provided as a param
        callback = callback || function(data) {
            this.dataReturned = data; // or something
        }
        // Assign it to the window object
        window[funcName] = function(data) {
             callback(data);
             // Unassign this function
             delete window[funcName];
             // Recycle the script tag
             document.body.removeChild(new_tag);
        }
        new_tag.type = 'text/javascript';
        new_tag.src = 'http://somesite.com/somemethod?callback='+funcName;
        // Add the element
        document.body.appendChild(new_tag);
     }
    

    Note you will have to make sure the JSONP request accepts the callback GET parameter. If you’re using a 3rd party API they will already support this. Hope this helps!

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

Sidebar

Related Questions

This whole Facebook access_token thing is driving me nuts. All I want to do
I'm new to this whole Joomla thing, and I'm trying to get an swf
(I'm a mac user) I'm new at this whole programing thing. its been explained
Alright, I thought I had this whole setTimeout thing perfect but I seem to
Yo. I'm really quite new to this whole JavaScript business, not to mention AJAX,
I have been struggling with this yesterday whole day, and still couldnt figure it
the whole point of this is to have transparent text with a light white
My whole purpose of this is to get something looking like this: I want
I'm quite new to the whole jQuery/JSON thing but thought I would give it
We have a web application (quite a heavy-weight one) that does a whole bundle

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.