Gino
02-28-2012, 01:48 PM
Hello,
Into an external js file, I'm trying to use the facebook graph api to read values of "shares" and "comments" and import them into js variables.
As example, using http://graph.facebook.com/?id=http://www.google.com in the browser, I get the following response:
{
"id": "http://www.google.com",
"shares": 3208837,
"comments": 2
}
In my js code, I need something like:
var val_shares=xx;
var val_comments=xx;
Here's the code I'm using to (try) do it:
function getNewHTTPObject()
{
var xmlhttp;
/** Special IE only code ... */
/*@cc_on
@if (@_jscript_version >= 5)
try
{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (E)
{
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
/** Every other browser on the planet */
if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
{
try
{
xmlhttp = new XMLHttpRequest();
}
catch (e)
{
xmlhttp = false;
}
}
return xmlhttp;
}
var xmlHttp = getNewHTTPObject();
function getDynamicData()
{
var url = "http://graph.facebook.com/?id=http://www.google.com";
xmlHttp.open('GET', url, true);
xmlHttp.onreadystatechange = callbackFunction;
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlHttp.send(null);
}
var syndLinkRequest = getNewHTTPObject();
function callbackFunction()
{
if (syndLinkRequest.readyState != 4)
return;
var result = xmlHttp.responseText;
}
into the HTML I call getDynamicData() at <body onload=getDynamicData()>...
Using Firefox in console mode, I see that the results arrive at this point of code:
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlHttp.send(null);
But I do not know (or understand) how and where to get those value to associate them to a js variable.
I understood I will need to use json to achieve this but I don't know where to start.
In addition, I can't use jQuery or php to do it.
After 2 days of searches, I'm about to give up.
Does anyone have an idea how to do or where I can find a concrete sample to show me the right way ?
Thanks a lot.
Gino
Into an external js file, I'm trying to use the facebook graph api to read values of "shares" and "comments" and import them into js variables.
As example, using http://graph.facebook.com/?id=http://www.google.com in the browser, I get the following response:
{
"id": "http://www.google.com",
"shares": 3208837,
"comments": 2
}
In my js code, I need something like:
var val_shares=xx;
var val_comments=xx;
Here's the code I'm using to (try) do it:
function getNewHTTPObject()
{
var xmlhttp;
/** Special IE only code ... */
/*@cc_on
@if (@_jscript_version >= 5)
try
{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (E)
{
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
/** Every other browser on the planet */
if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
{
try
{
xmlhttp = new XMLHttpRequest();
}
catch (e)
{
xmlhttp = false;
}
}
return xmlhttp;
}
var xmlHttp = getNewHTTPObject();
function getDynamicData()
{
var url = "http://graph.facebook.com/?id=http://www.google.com";
xmlHttp.open('GET', url, true);
xmlHttp.onreadystatechange = callbackFunction;
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlHttp.send(null);
}
var syndLinkRequest = getNewHTTPObject();
function callbackFunction()
{
if (syndLinkRequest.readyState != 4)
return;
var result = xmlHttp.responseText;
}
into the HTML I call getDynamicData() at <body onload=getDynamicData()>...
Using Firefox in console mode, I see that the results arrive at this point of code:
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlHttp.send(null);
But I do not know (or understand) how and where to get those value to associate them to a js variable.
I understood I will need to use json to achieve this but I don't know where to start.
In addition, I can't use jQuery or php to do it.
After 2 days of searches, I'm about to give up.
Does anyone have an idea how to do or where I can find a concrete sample to show me the right way ?
Thanks a lot.
Gino