views:

78

answers:

1

I'm using PHP to pull the events from a FullCalendar as a JSON feed but I need to support multiple domains. I need a querystring variable to specify which calendar to pull events from... is this causing a problem? Here's the FullCalendar init code:

$('#full-calendar".$id."').fullCalendar({
   editable: false,
   events:'http://www.mydomain.com/resources/include/calendar-events.php?cal=".$id."',
   loading: function(bool) {
       if (bool) $('#loading').show();
       else $('#loading').hide();
   }
});

The documentation says "If you need to access a feed that is in a different domain, you can use JSONP with a ? in your URL (see the JSONP discussion in $.ajax)."

But I'm not exactly sure how to do that.

Thanks for your help in advance.

+1  A: 

Well at a high level, JSONP lets you specify the name of a callback function that you want called when the AJAX request returns with data. HTTP GET operations can happen across different domains, (when you embed an image from a different host, you are creating an HTTP GET). POST (and PUT, DELETE etc) are limited to the same domain as the document (this is called the Same Origin Policy). JSONP, adds an extra parameter (usually 'callback') with the value of a JavaScript function in your document (the callback function). The sever sending the JSON needs to know to extract the value for that parameter. Your request might look like this:

 GET http://ical.example.com/cal.json?callback=_calDraw

The cal.json servlet will return this

 _calDraw({event:{date:'12/25/2010',title:'Jason\'s birthday'}});

Now this bit of JavaScript references the callback function you passed into it, but without a corresponding

  function _calDraw(data) {
     //render stuff
  }

The returned data will just fail. It's important that you have some level of trust with any server you are making a JSONP call to, because you are giving them permission to execute JavaScript in your document (they don't have to return something valid).

Hope this helps!

Jason Sperske