Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
922 views
in Technique[技术] by (71.8m points)

jquery - Google Feed Loader API ignoring XML attributes

Google's feed loader appears to be ignoring attributes when converting to JSON. I'm using jQuery to grab a feed via AJAX. The actual RSS XML feed can be seen here, and the response from the AJAX call can be seen here.

I need to access the urlattribute of the <enclosure> tags, but neither appear in the response.

For reference, the code I am using is:

function getFeed(url) {
    url = 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&callback=?&q=' 
            + encodeURIComponent(url);
    $.ajax({
        type: 'GET',
        url: url,
        dataType: 'jsonp',
        cache: false,
        success: function(d) { alert(JSON.stringify(d); },
        error: function(s,x) { alert(x); }
    }); 
}

I can't figure out how to get an XML response instead, as changing dataType : 'xml' causes an HTTP error. JSON is preferable.

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The 'enclosure' tag is not included in the JSON response, so you have two options setting the output argument:

You need to set the output to 'XML': https://developers.google.com/feed/v1/jsondevguide#json_args

url = 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&output=xml&num=10&callback=?&q='+ encodeURIComponent(url); 

Or use the mixed format: https://developers.google.com/feed/v1/devguide#resultMixed

url = 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&output=json_xml&num=10&callback=?&q='+ encodeURIComponent(url); 

You′ll get the JSON plus a a new xmlString property with all the tags (including 'enclosure' attributes)

alert(d.responseData.xmlString);

In both cases you need to parse the XML string and navigate to the needed info

Hope this helps


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...