Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Rendering Content as XML for AJAX


Go to End


3 Posts   1981 Views

Avatar
zenmonkey

Community Member, 545 Posts

31 December 2009 at 5:52am

I'm trying to create an AJAX image switcher for different Colored Product.

In my Page_Controller id added

if(Director::is_ajax() || $_GET["ajaxDebug"]) {
			$this->isAjax = true;
			}
			else {
				$this->isAjax = false;
				} 

Then in my ColorProduct Page Controller I added:

 function index() { 
  if($this->isAjax) {
   return $this->renderWith("ColorProductXML");
  }
  else {
   return Array();
  }  
 }

I then created a ColorProductXML tempalte and placed it in my root template directory

<?xml version="1.0" encoding="UTF-8"?>
<colorproduct>
	<% control Colors %>
	<color colorID="$StripColorName">
		<name>$ColorName</name>
		<hex>$ColorHex</hex>
		<% control ToyImageClasss %>
		<imagegroup imageName="$StripImageName">
			<type>$ImageClass</type>
			<% control ToyImages %>
				<% if Type=="Normal" %>
					<% control Attachment %>
						<thumbnail>$SetWidth(60).URL</thumbnail>
						<small>$SetWidth(460).URL</small>
						<zoom>$SetWidth(920).URL</zoom>
						<full>$URL</full>
					<% end_control %>
				<% end_if %>
				<% if Type=="Blur" %>
					<blur>$Attachment.URL</blur>
				<% end_if %>
			<% end_control %>
		</imagegroup>
		<% end_control %>
	</color>
	<% end_control %>
</colorproduct>

When I access a ColorProduct page adding ?ajax=1 to the end of my URL I get nice XML document, and my jQuery call to that url also returns a document in the same format but I can't query the nodes. Firebug shows the Content-Type as text/html instead of application/xml. How do I force SS to return an XML content type?

Avatar
zenmonkey

Community Member, 545 Posts

31 December 2009 at 11:00am

After a few more fruitless hours of digging I decided to copy the contents of the page with ?ajax=1 variable added and paste it into a static xml file. When my jQuery script loads THAT file i notice when I dump it i see the <?xml version="1.0" encoding="UTF-8"?> header at the top of the results but not from the "live" version

Avatar
zenmonkey

Community Member, 545 Posts

6 January 2010 at 4:24am

Any ideas? Enabling API access will give me an XML of the table, but I need access to resized image URLs