views:

899

answers:

1

I'm writing an application that downloads an XML string from a URL and POSTs that to another URL (that's set up to handle an incoming "XML" field). I've got the first part right - it downloads the XML and I can alert() it and all that, but I'm unable to figure out how to POST that data to a server.

function pull() {
    var myLoader = new air.URLLoader();
    var myRequest = new air.URLRequest('http://something/something.xml');
    myLoader.addEventListener(air.Event.COMPLETE, pulled);
    myLoader.load(myRequest);
}

function pulled(evt) {
    if (evt.target.bytesTotal>0) {
        // alerting shows the full string just fine
        alert(evt.target.data);

        var myLoader = new air.URLLoader();
        var myRequest = new air.URLRequest('http://someplace/push.php');
        myRequest.method = air.URLRequestMethod.POST;
        // myVars = new air.URLVariables("xml="+evt.target.data); // 
        // alert(evt.target.data.toUpperCase());
        myRequest.data = "xml="+evt.target.data; // myVars;
        myLoader.dataFormat = air.URLLoaderDataFormat.TEXT;
        myLoader.addEventListener(air.Event.COMPLETE, pushed);
        myLoader.load(myRequest);
    }
}

I made the 2nd server PHP echo the contents of the xml variable, but I'm just unable to get the exact contents of the XML string. There is something I'm doing wring with the myRequest.data and/or dataFormat bit.

Can someone just figure this out? I know it's probably a simple thing, but I'm at my wit's end right now.

This is my first AIR app.

Another related question (or sub-question) is that...

alert(evt.target.data);               // shows an alert box with the XML
alert(typeof evt.target.data);        // shows String
alert(evt.target.data.toUpperCase()); // shows the xml converted to upper case
alert(encodeURI(evt.target.data));    // shows up blank.
alert(escape(evt.target.data));       // shows up blank.

Why??

+1  A: 

The error seems to be the way you are assigning the parameters to 'data' ... Use URLVariables.

var params:URLVariables = new URLVariables();
params.[name of parameter] = [value];

--- so like params.xml = (YOUR XML) ... from your example:

// uses the dynamic object to add the 'xml' property to 'params' at runtime.
params.xml = evt.target.data

Then Change you request.data to request.data = params;

-- The URLVariables guy is dynamic - so you can add properties as I describe above.

For a basic example - much more complete that what I have here: http://livedocs.adobe.com/flex/3/html/help.html?content=data_access_2.html

Gabriel
I'm not actually using the content-type anywhere in the server either. I'm just taking in a string and running simplexml_load_string over it in the php.Forget the fact that this is xml. Considering it's just text, what changes would I need to make to get it to post the data?
aalaap
About how the data is expected, it's a simple `<FORM method="POST"...>` that I want to emulate. The code in the PHP uses a simple `$xml = $_POST['xml']` to access the post data. No content-type specs, no headers, nothing.
aalaap
I remember trying something similar to what you just suggested, but I will try it again. Do I leave the `myLoader.dataFormat = air.URLLoaderDataFormat.TEXT` intact or should I remove or change it to something else?
aalaap
Yep. Leave it as URLLoaderDataFormat.TEXT.
Gabriel
It's working! I removed the `myloader.dataFormat...` line (so it's taking the default value) and it's posting now.Thanks!!!
aalaap
Getting ready to hit the stack - if this doesn't work for you - post a comment and I'm happy to help tomorrow evening.
Gabriel
Cool! Happy to hear it.
Gabriel