如果将resultFormat的resultFormat设置为text,如何获取HTTPService的String响应文本?

时间:2013-12-09 09:13:03

标签: actionscript-3 flex actionscript

我想得到HTTPService调用的PHP页面的文本响应:

<?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 !";
}
?>

在我的flex代码中,我尝试得到这样的响应:

...
<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.
            }

但是我收到了这个错误:TypeError: Error #1009: Cannot access a property or method of a null object reference.

那么如何获得文本回复?

1 个答案:

答案 0 :(得分:2)

您应该在事件处理程序中设置结果:

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

现在您在服务调用之前使用lastResult属性。这就是为什么它是空的。