After making a cross-domain JSON request using YQL, I received the JSON code enclosed in a table <div>
within the html file.
The challenge now is figuring out how to extract this data and display it in a table format.
Below is the JavaScript code snippet:
// JavaScript Document
$(document).ready(function(){
var container = $('#target');
$('.ajaxtrigger').click(function(){
doAjax($(this).attr('href'));
return false;
});
function doAjax(url){
// check if it is an external URI
if(url.match('^http')){
// make a call to YQL
$.getJSON("http://query.yahooapis.com/v1/public/yql?"+
"q=select%20*%20from%20html%20where%20url%3D%22"+
encodeURIComponent(url)+
"%22&format=xml'&callback=?",
// function to handle the fetched data from JSON-P call
function(data){
// filter and render the data if available
if(data.results[0]){
var filteredData = filterData(data.results[0]);
container.html(filteredData);
// indicate if there was an error loading the page
} else {
var errormsg = "<p>Error: can't load the page.</p>";
container.html(errormsg);
}
}
);
// If not an external URI, utilize Ajax load()
} else {
$('#target').load(url);
}
}
// Function to filter out unwanted elements
function filterData(data){
data = data.replace(/<?\/body[^>]*>/g,'');
data = data.replace(/[\r|\n]+/g,'');
data = data.replace(/<--[\S\s]*?-->/g,'');
data = data.replace(/<noscript[^>]*>[\S\s]*?<\/noscript>/g,'');
data = data.replace(/<script[^>]*>[\S\s]*?<\/script>/g,'');
data = data.replace(/<script.*\/>/,'');
return data;
}
});
And here is the corresponding HTML code:
<body>
<div id="doc" class="yui-t7">
... (HTML content)
</body>
In attempting to manipulate the retrieved data, the following code section didn't work as expected:
// $.getJSON(data, function(json){
// decipher the response structure here...
//
document.getElementById("placeholder").innerHTML=prova.buy.currency+" "+prova.sell.currency+" "+prova.offer[0].amount+" "+prova.offer[0].rate+" "+prova.offer[0].seller.name;
(UPDATE) Following your suggestions, I ran a test using the below:
// TEST
$.getJSON("http://query.yahooapis.com/v1/public/yql?"+
"q=select%20*%20from%20html%20where%20url%3D%22"+
encodeURIComponent(url)+
"%22&format=json'&callback=?",
// handling the data from successful JSON-P call
function(data){
document.getElementById('placeholder').value = JSON.stringify(data,null,' ');
// filtering and rendering data if available
if(data.results[0]){
var data = filterData(data.results[0]);
container.html(data);
alert(data);
// indicating errors when accessing the page
} else {
var errormsg = "<p>Error: can't load the page.</p>";
container.html(errormsg);
}
}
);
However, the code stopped at the alert(data) point without proceeding to the part involving document.getElementById.
I also switched from using a "xml" request to a "json" request...
SECOND UPDATE
Resolved the issue with the 'div id=placeholder' in the HTML table. It seems there were challenges with this div, as changing the 'div id' to 'texture id=placeholder' made it work.
With the entire JSON string in my text area now, I attempted to use the getJson command to extract a specific portion of the data and display it in a table but encountered some difficulties.
I'm unable to comprehend why, despite having a json code, I am facing obstacles in extracting and displaying the required part using the suggested code.
FINAL PARTIAL UPDATE
The issue was identified in the "data" filter where it failed to remove "" tags from the data, resulting in parse.Json(data) being incapable of interpreting the format!
Successfully retrieved the necessary information.
Here's the final .js code snippet:
// JavaScript Document
$(document).ready(function(){
var container = $('#target');
$('.ajaxtrigger').click(function(){
doAjax($(this).attr('href'));
return false;
});
function doAjax(url){
// if it is an external URI
if(url.match('^http')){
// call YQL
// TEST
$.getJSON("http://query.yahooapis.com/v1/public/yql?"+
"q=select%20*%20from%20html%20where%20url%3D%22"+
encodeURIComponent(url)+
"%22&format=json'&callback=?",
// handling the data from successful JSON-P call
function(data){
// filter and render the data
if(data.results[0]){
**var data = filterData(data.results[0]);**
container.html(data);
alert(data);
document.getElementById("prova1").value = data;
var obj = $.parseJSON(data);
alert(obj.sell.currency);
// END OF TEST
// indicating errors when accessing the page
} else {
var errormsg = "<p>Error: can't load the page.</p>";
container.html(errormsg);
}
}
);
// if it is not an external URI, use Ajax load()
} else {
$('#target').load(url);
}
}
// filter out unwanted elements
function filterData(data){
**data = data.replace(/<body>/,'');**
data = data.replace(/<?\/body[^>]*>/g,'');
data = data.replace(/[\r|\n]+/g,'');
data = data.replace(/<--[\S\s]*?-->/g,'');
data = data.replace(/<noscript[^>]*>[\S\s]*?<\/noscript>/g,'');
data = data.replace(/<script[^>]*>[\S\s]*?<\/script>/g,'');
data = data.replace(/<script.*\/>/,'');
return data;
}
});