1

我想向 Posts 控制器发送一些数据(图像、文本、...):

    $('#home').click(function (){
        var xhr = new XMLHttpRequest();
        xhr.open("POST","/Portfilo/posts/test",true);
        xhr.send("id=10");
        xhr.onreadystatechange=function()
        {
            if (xhr.readyState==4 && xhr.status==200)
            {
                alert(xhr.responseText);
            }
        }
    });

测试动作是:

    public function test()
    {
        $this->layout = 'ajax';
        //$id = $this->params['named']['id'];


        if($this->request->named){
            echo "Yesssssss";
        }
        else {
            echo 'Oh No';
        }

    }

我如何从这个连接(xmlhttprequest)中检索这些数据。

我读了这篇 文章,但是像这样的函数或属性:

// Passed arguments

$this->request->pass;

$this->request['pass'];

$this->request->params['pass'];

或者

// named parameters

$this->request->named;

返回给我“哦 Noooo”消息。

如何从这个请求中检索这些参数和数据?

4

1 回答 1

1

如果您通过POST方法向test动作发送数据,您可以使用$this->request->data数组检索它,该数组将包含由POST.

在你的情况下,你可以试试这个:

public function test(){
        $this->layout = 'ajax';

        if($this->request->data['id']){
            echo "Yesssssss";
        }
        else {
            echo 'Oh No';
        }
}
于 2012-12-07T11:39:26.943 回答