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

The Archive Base Latest Questions

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

I have a javascript code on my website, there is a variable: var remoteJsonVar;

  • 0

I have a javascript code on my website, there is a variable:

var remoteJsonVar;

On the other hand there is a json file on a remote website

https://graph.facebook.com/?ids=http://www.stackoverflow.com

I need to set the variable remoteJsonVar to this remote jason data.

I am sure that it is very simple, but I can’t find the solution.

A small working example would be nice.

  • 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-30T09:17:11+00:00Added an answer on May 30, 2026 at 9:17 am

    Because you’re trying to get the data from a different origin, if you want to do this entirely client-side, you’d use JSON-P rather than just JSON because of the Same Origin Policy. Facebook supports this if you just add a callback parameter to your query string, e.g.:

    https://graph.facebook.com/?ids=http://www.stackoverflow.com?callback=foo
    

    Then you define a function in your script (at global scope) which has the name you give in that callback parameter, like this:

    function foo(data) {
        remoteJsonVar = data;
    }
    

    You trigger it by creating a script element and setting the src to the desired URL, e.g.:

    var script = document.createElement('script');
    script.src = "https://graph.facebook.com/?ids=http://www.stackoverflow.com?callback=foo";
    document.documentElement.appendChild(script);
    

    Note that the call to your function will be asynchronous.

    Now, since you may want to have more than one outstanding request, and you probably don’t want to leave that callback lying around when you’re done, you may want to be a bit more sophisticated and create a random callback name, etc. Here’s a complete example:

    Live copy | Live source

    (function() {
    
      // Your variable; if you prefer, it could be a global,
      // but I try to avoid globals where I can
      var responseJsonVar;
    
      // Hook up the button
      hookEvent(document.getElementById("theButton"),
                "click",
                function() {
          var callbackName, script;
    
          // Get a random name for our callback
          callbackName = "foo" + new Date().getTime() + Math.floor(Math.random() * 10000);
    
          // Create it
          window[callbackName] = function(data) {
              responseJsonVar = data;
              display("Got the data, <code>shares = " +
                data["http://www.stackoverflow.com"].shares +
                "</code>");
    
              // Remove our callback (`delete` with `window` properties
              // fails on some versions of IE, so we fall back to setting
              // the property to `undefined` if that happens)
              try {
                  delete window[callbackName];
              }
              catch (e) {
                  window[callbackName] = undefined;
              }
          }
    
          // Do the JSONP request
          script = document.createElement('script');
          script.src = "https://graph.facebook.com/?ids=http://www.stackoverflow.com&callback=" + callbackName;
          document.documentElement.appendChild(script);
          display("Request started");
      });
    
      // === Basic utility functions
    
      function display(msg) {
        var p = document.createElement('p');
        p.innerHTML = msg;
        document.body.appendChild(p);
      }
    
      function hookEvent(element, eventName, handler) {
        // Very quick-and-dirty, recommend using a proper library,
        // this is just for the purposes of the example.
        if (typeof element.addEventListener !== "undefined") {
          element.addEventListener(eventName, handler, false);
        }
        else if (typeof element.attachEvent !== "undefined") {
          element.attachEvent("on" + eventName, function(event) {
            return handler(event || window.event);
          });
        }
        else {
          throw "Browser not supported.";
        }
      }
    })();
    

    Note that when you use JSONP, you’re putting a lot of trust in the site at the other end. Technically, JSONP isn’t JSON at all, it’s giving the remote site the opportunity to run code on your page. If you trust the other end, great, but just remember the potential for abuse.

    You haven’t mentioned using any libraries, so I haven’t used any above, but I would recommend looking at a good JavaScript library like jQuery, Prototype, YUI, Closure, or any of several others. A lot of the code above has already been written for you with a good library. For instance, here’s the above using jQuery:

    Live copy | Live source

    jQuery(function($) {
    
      // Your variable
      var responseJsonVar;
    
      $("#theButton").click(function() {
        display("Sending request");
        $.get("https://graph.facebook.com/?ids=http://www.stackoverflow.com&callback=?",
              function(data) {
                responseJsonVar = data;
                display("Got the data, <code>shares = " +
                  data["http://www.stackoverflow.com"].shares +
                  "</code>");
              },
              "jsonp");
      });
    
      function display(msg) {
        $("<p>").html(msg).appendTo(document.body);
      }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have javascript code embedded inside a html template file. When I load this
I have JavaScript code which copies the value of input file and paste it
I have this JavaScript code: for (var idx in data) { var row =
I have a website to which i've added dynamic javascript code generated on server.
I have a website on which I dynamically create Javascript code using ASP.NET handler
I have a problem with old website. All JavaScript code on it use getElemenById
i have this javascript code to which i want to add the jquery fade-in
I have some Javascript code that will programmatically register an COM interop assembly by
I have a javascript code, whereby I'm trying to load a list from a
I have this javascript code below that uses jquery, it is suppoed to be

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.