I have the following script which is able to access a jsonp feed and get data from it:
$(document).ready(function() {
get_jsonp_feed();
function get_jsonp_feed() {
$.ajax({
url: "http://query.yahooapis.com/v1/public/yql?q=%20SELECT%20*%20FROM%20html%20WHERE%20url%3D%22http%3A%2F%2Fnews.bbc.co.uk%2Fweather%2Fforecast%2F4276%3Fsearch%3Dgerrards%2520cross%26itemsPerPage%3D10%26region%3Dworld%26area%3DGerrards%2520Cross%22%20and%20xpath%20%3D%20'%2F%2Ftable%2Ftbody%2Ft开发者_开发知识库r'&format=json&diagnostics=true&callback=cbfunc",
type: 'GET',
dataType: 'jsonp',
jsonp: 'callback',
jsonpCallback: 'cbfunc',
error: function(xhr, status, error) {
alert(xhr.responseText);
},
success: function(data) {
var itemList = data.query;
alert(itemList.count);
/*
var buildHTML = [];
for (var i = 0; i < 5; i++) {
buildHTML.push('<div class="container"><a href="' + itemList[i].link + '" class="title" target="_blank">' + itemList[i].title + '<br /><span class="dateandtime">' + itemList[i].pubDate + '</span></a></div>');
}
$('.portlet-content').empty().append(buildHTML.join('<hr />'))
*/
}
});
}
});
That gives me 5
which is right. I now want to get data which is more embedded in the jsonp, but I have having difficulty. For example, I want to get:
data.query.results.tr.td.div.abbr.title
How would I go about getting that?
Here is a jsfiddle of it returning 5: http://jsfiddle.net/B6hRG/1/
Here is some fixed code to get JSONP instead of XML:
$(document).ready(function() {
get_jsonp_feed();
function get_jsonp_feed() {
$.ajax({
url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%20%3D%20'http%3A%2F%2Fnews.bbc.co.uk%2Fweather%2Fforecast%2F4276%3F%26search%3Dgerrards%2520cross%26itemsPerPage%3D10%26region%3Dworld%26area%3DGerrards%2520Cross'%20and%20xpath%20%3D%20'%2F%2Ftbody%2Ftr'&format=json&callback=?",
type: 'GET',
dataType: 'jsonp',
error: function(xhr, status, error) {
alert(xhr.responseText);
},
success: function(data) {
var itemList = data.query;
alert(itemList.count);
/*
var buildHTML = [];
for (var i = 0; i < 5; i++) {
buildHTML.push('<div class="container"><a href="' + itemList[i].link + '" class="title" target="_blank">' + itemList[i].title + '<br /><span class="dateandtime">' + itemList[i].pubDate + '</span></a></div>');
}
$('.portlet-content').empty().append(buildHTML.join('<hr />'))
*/
}
});
}
});
To get data like data.query.results.tr.td.div.abbr.title
you'd have to either use a for
loop on the data.query.results.tr.td
object, and any others with siblings, or use XML and get the raw html, put it into a document fragment and use jQuery on that.
精彩评论