Parsing Google XML Calendar with JQuery
I am trying to parse a google calendar calendar like this:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<p id="entries"></p>
<script>
$(document).ready(function(){
$.ajax({
url: 'http://www.google.com/calendar/feeds/339if8qgnu8pic5a2ru4moe5m0%40group.calendar.google.com/public/full',
dataType: 'xml',
success: parseXML
});
function parseXML(xml){
$(xml).find('entry').each( function() {
$('#entries').append($(this).find('title').text() + '<br />')
});
}
});
</script>
</body>
</html>
But it won't find any items. What am I doing wrong? Tried the same code with a simpler XML file that worked ...
+3
source to share
1 answer
When I tested myself, it seemed like it would not find any items because there was no data returned by the query. I thought that of course I was missing something due to the comments about incorrect encoding ... but I downloaded the XML document and saved it next to the HTML file you provided, making the following changes:
url: 'full.xml',Having done this, (for me) the paragraph element is now populated with two entries, which can be seen in the XML targeting. Of course this is due to cross-domain policies, and what is not?
You have several options for capturing the XML correctly:
- PHP server file file_get_contents () / cURL
→ The file can now be AJAXed on your page as you are on the same domain.
→ This server side request can also be part of this page, that is: When the page is loaded, PHP / Whatever sounds like a variable in JavaScript or in a hidden element.
<w> - Modified AJAX request
→ Modified AJAX request that will work with cross-domain requests. This is useful as the server will not fulfill the request you might want. If so, then I must mention; I have a file namedjquery.xdomainajax.js
that allows cross-domain AJAX requests. I'm looking for a source at the moment, but I figured I'd rush this post so you can look for yourself if you like.
Edit: select this one .
Edit2: After some brief and sloppy testing, I can't seem to get it to work using the above jQuery plugin ... so my solution would be to clean it up on the server side.
Here are some more links:
- How to load a specific div of an external web page in another web page that is on a different domain
- Get website titles
- JQuery ajax domain cross call and permission
FYI: I am using FireFox 12.0
+1
source to share