pheromix
pheromix

Reputation: 19307

How to get the String response text of a HTTPService if its resultFormat is set to text?

I want to get the text response of the PHP page which was called by a HTTPService :

<?php
if ($_POST['col1'] && $_POST['col2']) {
    $handle = fopen ("C:\NR.txt", "a+");
    $x = "colonne 1 : ".$_POST['col1']."\r\n";
    $x .= "colonne 2 : ".$_POST['col2']."\r\n";
    fwrite($handle, $x);
    fclose ($handle);
    echo "success !"; // I want to get this text response
}
else {
    echo "pas de données venant de flex !";
}
?>

In my flex code I tried getting the response like this :

...
<mx:HTTPService id="userRequest" url="http://localhost/tabletteNR/NR.php" resultFormat="text" useProxy="false" method="POST" />
...
protected function button_clickHandler_send(event:MouseEvent):void
            {
                userRequest.cancel();
                var params:Object = new Object();
                params.col1 = so.data["champ1"];
                params.col2 = so.data["champ2"];
                userRequest.send(params);
                resultHTTP.text = userRequest.lastResult.toString(); // here I want to show in a TextArea the response String.
            }

but I got this error : TypeError: Error #1009: Cannot access a property or method of a null object reference.

So how to get the text response ?

Upvotes: 0

Views: 202

Answers (1)

Andrei
Andrei

Reputation: 571

You should set result inside event handler:

<s:HTTPService id="userRequest" result="userRequest_resultHandler(event)" ... />
...
protected function userRequest_resultHandler(event:ResultEvent):void
{
    resultHTTP.text = userRequest.lastResult.toString();
}

Now you are using lastResult property before service call. That's why it's null.

Upvotes: 2

Related Questions