Torkashvand
Torkashvand

Reputation: 416

Retrieve data sent by xmlhttprequest in cakephp's controller

I want to send some data (image,text,...) to Posts controller :

    $('#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);
            }
        }
    });

test action is :

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


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

    }

how I cane retrieve these data from this connection (xmlhttprequest).

I read this article but functions or properties like these :

// Passed arguments

$this->request->pass;

$this->request['pass'];

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

Or

// named parameters

$this->request->named;

return to me "Oh Noooo" message.

How to retrieve these parameter and data s from this request?

Upvotes: 1

Views: 1024

Answers (1)

Alvaro
Alvaro

Reputation: 41595

If you are sending data through POST method to the test action, you can retrieve it using $this->request->data array, which will contain all variables sent by POST.

In your case, you can try this:

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

        if($this->request->data['id']){
            echo "Yesssssss";
        }
        else {
            echo 'Oh No';
        }
}

Upvotes: 1

Related Questions