PDA

View Full Version : How Do I Set XML Content-type in Flex 3?


Laxmidi
06-11-2010, 07:23 PM
Hi,

My Flex 3 project makes an ExternalInterface call to some Javascript. The Javascript is then turned into XML.

But, my xml isn't parsing in IE. It works in all other browsers. I think that the problem is that I haven't set the XML's content-type. As I understand it IE requires that the Content-type be set. Is this correct?

My code looks like:


myReturn = '<myXMLReturn>' + myReturn + '</myXMLReturn>';

myReturn = '<?xml version="1.0" encoding="UTF-8"?>' + myReturn;

xmlReturn = new XML(myReturn);


How do I set: header('Content-type: application/xml'); for an ExternalInterface call?

Thank you.
-Laxmidi

tadster
06-11-2010, 08:45 PM
Isn't parsing? Are you getting the ExternalInterface._toAS Error? I bet.

And then, are there any ampersands (&) in your inner xml (myReturn)?
Things like &nbsp;
will mess up IE (and some other browsers) when dealing with xml from js to as.

Anyway, one solution I've found is to put a try catch around your ExternalInterface call, the thing is that the _toAS method happens well after the call has already gone through...so, I'm not too sure what it does, but you can try catch it out.

Laxmidi
06-12-2010, 02:53 PM
Hi Tadster,

Thanks for the message. The weird thing is that I'm getting no error messages.

It parses in all other browsers, but silently dies in IE.

Also, the XML validates. I escaped all of the '&'. If there were an unescaped non-breaking space would it validate?

Does IE require a special header or content type? Or maybe it doesn't like parsing with the ..@.. format?

Any suggestions?

Thank you.

-Laxmidi

tadster
06-13-2010, 04:59 PM
Could you post more of your code?
I'd like to see how the ExternalInterface comes into play here; and what do you mean by parsing? I can take that a couple ways, and am unclear as to exactly what you mean, the text does not display?

Laxmidi
06-13-2010, 06:38 PM
Hi Tadster,

Thanks so much for taking an interest in my problem. I use the class below to grab the Javascript and turn the external inteface call into XML. The return from Javascript is poorly formatted, so I have to fix it before turning it into XML. Please find below my code:

package com.darylbowden.ads
{
import flash.external.ExternalInterface;

import mx.controls.Alert;
import mx.utils.ObjectUtil;

public class OpenAds {

private static var externalCallback:String = "callOpenAds";
private static var retries:uint = 10;

public function OpenAds(){
}

public function getAdReturn():Ad {
var xmlReturn:XML;
if(ExternalInterface.available) {
var adReturn:String = ExternalInterface.call(externalCallback);


try

{
if( adReturn.charCodeAt( 0 ) == 65279 )
{
adReturn = adReturn.substr( 1 );
}
adReturn = adReturn.replace("//]]>->","//]]>->\n");
var infoTagsIndex:int = adReturn.indexOf('<script type="text/javascript" src=""', 0);
adReturn = adReturn.substring(infoTagsIndex, adReturn.length);
var wasFlash:Boolean = false;
if(adReturn.search("<embed") == -1) {
adReturn = adReturn.replace('></a>', '></img></a>');
adReturn = adReturn.replace( '/></img></a>', '></img></a>' );
adReturn = adReturn.replace('></div>', '></img></div>');
adReturn = adReturn.replace( '/></img></div>', '></img></div>' );
adReturn = adReturn.replace( '&contenttype=png', '&amp;contenttype=png' );
adReturn = adReturn.replace( 'filename=mybanner.png&contenttype=png', 'filename=mybanner.png&amp;contenttype=png');
adReturn = adReturn.replace( 'target=_blank', 'target="_blank"');
adReturn = adReturn.replace( 'border=0', 'border="0"');
adReturn = adReturn.replace( 'width=468', 'width="468"');
adReturn = adReturn.replace( 'height=60', 'height="60"/');
adReturn = adReturn.replace( 'width=0', 'width="0"');
adReturn = adReturn.replace( 'height=0', 'height="0"/');



var myRegExp1:RegExp = new RegExp("type=text/javascript", "g");
adReturn = adReturn.replace(myRegExp1, 'type="text/javascript"');

var myRegExp2:RegExp = /id=beacon_[0-9a-z]{10}/;
var myRegExp3:RegExp = /beacon_[0-9a-zA-Z]{10}/;
var matchedPattern:Array = adReturn.match(myRegExp2);
var matchedPattern2:Array = adReturn.match(myRegExp3);
if (matchedPattern != null) {adReturn = adReturn.replace(myRegExp2, 'id="'+matchedPattern2[0]+'"'); }

}

else {
adReturn = adReturn.replace('></div>\n<script','></embed></div>\n<script');
adReturn = adReturn.replace('/></embed></div>\n<script','></embed></div>\n<script');
adReturn = adReturn.replace('></div>\n<noscript','></img></div>\n<noscript');
adReturn = adReturn.replace('/></img></div>\n<noscript','></img></div>\n<noscript');
wasFlash = true;
}



adReturn = '<adXMLReturn>' + adReturn + '</adXMLReturn>';


xmlReturn = new XML(adReturn);

var ad:Ad;

if(wasFlash){
ad = new Ad(xmlReturn, true);
}

else {
ad = new Ad(xmlReturn);
}
}

catch(error:Error){

Alert.show(error.message + 'stacktrace: ' + error.getStackTrace(), "Error");
}
}
else {
Alert.show("Javascript must be enabled to view this page properly.", "Javascript Not Detected");
xmlReturn = new XML('<root>xml</root>');
}


return ad;
}

}



}

In my Ad.as class the clickURL and imageSource are set as follows:
_clickURL = xhtml.a.@href.toString();
_mediaSource = xhtml.a.img.@src.toString();

For some reason, these two variables end up as "" in IE.

Everything works in IE If I hard code it like this:
_clickURL = "http://www.mywebsite.com/openx/www/delivery/ck.php?oaparams=2__bannerid=1__zoneid=4__cb=305899 7a64__oadest=http%3A%2F%2Fwww.mywebsite.com";

_mediaSource = "http://www.mywebsite.com/openx/www/delivery/ai.php?filename=mybanner.png&amp;contenttype=png";


So for some reason it dies silently in IE. I don't get my ad and I don't get any error messages. The problem appears to be that IE can't parse the XML for some reason.

If you have any ideas or suggestions, please let me know. I'd love to hear them.

Thank you!

-Laxmidi

Laxmidi
06-13-2010, 06:43 PM
Hi Tadster,

I've also included the XML created by the OpenAds class. Please see below:

<adXMLReturn>
<SCRIPT type="text/javascript"/>
<SCRIPT type="text/javascript" src="http://www.mywebsite.com/openx/www/delivery/ajs.php?zoneid=4&amp;cb=78244247341&amp;charset=utf-8&amp;loc=http%3A//www.mywebsite.com/"/>
<A href="http://www.mywebsite.com/openx/www/delivery/ck.php?oaparams=2__bannerid=1__zoneid=4__cb=0416e6 03aa__oadest=http%3A%2F%2Fwww.mywebsite.com" target="_blank">
<IMG title="" border="0" alt="" src="http://www.mywebsite.com/openx/www/delivery/ai.php?filename=mybanner.png&amp;contenttype=png" width="468" height="60"/>
</A>
<DIV style="POSITION: absolute; VISIBILITY: hidden; TOP: 0px; LEFT: 0px" id="beacon_0416e603aa">
<IMG style="WIDTH: 0px; HEIGHT: 0px" alt="" src="http://www.mywebsite.com/openx/www/delivery/lg.php?bannerid=1&amp;campaignid=1&amp;zoneid=4&amp;loc=http%3 A%2F%2Fwww.mywebsite.com%2F&amp;cb=0416e603aa" width="0" height="0"/>
</DIV>
<NOSCRIPT/>
</adXMLReturn>


Thank you!

-Laxmidi

tadster
06-13-2010, 08:07 PM
hmmm... i'm not sure but in IE it may be only seeing it as capital A.. for the anchor and imagae, so

_clickURL = xhtml.A.@href.toString();
_mediaSource = xhtml.A.IMG.@src.toString();

maybe...
And, what does all this do? That 'xml' looks just like some html inside one xml tag.
I feel there may be an easier way to do what your doing. But I'm not sure of what your doing... Also, the noscript tag may have something to do with it. You can also try getting rid of it and see what happens.

Laxmidi
06-13-2010, 10:32 PM
Hi Tadster,

Thank you for looking over my code.

I'm trying to get OpenX www.openx.org (http://www.openx.org), which is an ad server to work with Flex.

So, I don't have true control over what OpenX throws at me. That's why there is this convoluted way of getting the External Interface call result into well-formatted XML.

I'll try using lower-case letters in setting _clickurl and _mediasource. I never thought of that. I'll give it a whirl.

Again, thank you so much for your advice.

-Laxmidi

tadster
06-13-2010, 11:03 PM
hmmm... ok, would it be all right to just pull the data straight from the string that the callOpenAds function returns? Something like this:

//...
var adReturn:String = ExternalInterface.call(externalCallback);
var aURL:RegExp = /http:[\/]{2}.[\"]{1}/i;
var link:String = adReturn.match(aURL).toString().replace("\"", "");
//so that would be the very first url match in the string

var imgLink:String = adReturn.replace(link, "").match(aURL).toString().replace("\"", "");
//and since the img link is second, the code above would find it

//...
_clickURL = link;
_mediaSource = imgLink;


I guess my question is why does the Ad Class need xml passed to it?

Laxmidi
06-14-2010, 07:07 PM
Hi Tadster,

Below I've included Ad.as

package com.darylbowden.ads
{
import mx.controls.Alert;
import mx.utils.ObjectUtil;


public class Ad extends Object {
private var _clickURL:String;
private var _mediaSource:String;
private var _target:String;
private var _flash:Boolean;

public function Ad(xhtml:XML, flash:Boolean = false) {
super();
_flash = flash;
_parse(xhtml);


}

private function _parse(xhtml:XML):void {


if(!_flash) {
_clickURL = xhtml.a.@href.toString();
_mediaSource = xhtml.a.img.@src.toString();
}

else if (_flash) {
_mediaSource = xhtml.div.embed.@src.toString();

var clickIndex:uint = _mediaSource.indexOf('clickTAG=', 0);
_clickURL = _mediaSource.substring( clickIndex+9, _mediaSource.length);
}
}

public function get clickURL():String {
return _clickURL;

}

public function get mediaSource():String {

return _mediaSource;

}

public function get target():String {

return _target;
}

}




}


It's weird that these 2 lines:
_clickURL = xhtml.a.@href.toString();
_mediaSource = xhtml.a.img.@src.toString();

don't parse. The just produce "" in IE even though they work in the other browsers.

Thank you!

-Laxmidi

tadster
06-14-2010, 09:09 PM
Ah! The problem is in the callOpenAds javascript function... let's see that.

Laxmidi
06-14-2010, 09:25 PM
Tadster,

You nailed it!!!! It was case sensitivity.

Works in Safari, et al.:
_clickURL = xhtml.a.@href.toString();
_mediaSource = xhtml.a.img.@src.toString();


Works in IE:
_clickURL = xhtml.A.@href.toString();
_mediaSource = xhtml.A.IMG.@src.toString();

When I looked at the xmlReturn from Safari and Firefox, they are the same, except that IE capitalized the html tags. Safari kept the tags lower-case.

Thank you SO MUCH for your generous help. I was screwing around with this problem for literally weeks. I'm so psyched!!!! Sweeeeeet!!!!!

Again, THANK YOU.

-Laxmidi

tadster
06-15-2010, 08:14 PM
cool, I'm glad I could help. :)