Wednesday, October 14, 2015

Re: How do I get Cake 3.1 to reply to an AJAX request without requiring a layout .ctp file?

code that generated the URL:
$url = $this->Url->build(['controller' => 'Purchases', 'action' => 'get_sales_people_select_list','_ext'=>'json']);
$url = $url . '/47';

The generated URL:
/purchases/get-sales-people-select-list.json/47

Is this not correct?

On Wed, Oct 14, 2015 at 4:40 PM, Anthony GRASSIOT <antograssiot@gmail.com> wrote:

Did you correctly specify the data type json in your ajax call ?

Le 14 oct. 2015 09:02, "heavyKevy" <arnett.kevin@gmail.com> a écrit :
I have gone through the documentation, which is a bit too vague, and tried many things, but I still am getting an error that the template file is missing.

I had this working sending back a json response in 3.0 using ext='json', yet after updating to 3.1 it is broken. 

I checked the migration guide and tried changing ext  to _ext, setting up the Routes with:
Router::extensions(['json']);

in the controller method I am using the _serialize key which is supposed to render without the template:
$this->set('_serialize', ['salesPeople']);

The JsonView class does not appear to be loading and I don't see in the documentation where I am supposed to set it.
The documentation says ' you can automatically leverage the new view classes' By enabling RequestHandlerComponent in your application, and enabling support for the json and or xml extensions, but I have enabled it in the Initialize method of the controller and enabled the 'json' extension as mentioned earlier and it is still not working.

Here is the method in question:
public function getSalesPeopleSelectList() {
        $this->request->accepts('json'); // No direct access via browser URL
        $dealerId = $this->request->params['pass'][0];
        $this->RequestHandler->renderAs($this, 'ajax');
        //$this->viewClass = 'Json';
        $this->loadModel('SalesPeople');
        $salesPeople = $this->Purchases->SalesPeople->find('list')->where(['is_active'=>'1','user_id'=>$dealerId]);  //for salespeople options for autocomplete
        $this->set(compact('salesPeople'));
        $this->set('_serialize', ['salesPeople']);
    }


When I enable the _ext = json I get the following error:

2015-10-14 06:57:36 Error: [Cake\Controller\Exception\MissingActionException] Action PurchasesController::getSalesPeopleSelectList.json() could not be found, or is not accessible.
Exception Attributes: array (
  'controller' => 'PurchasesController',
  'action' => 'getSalesPeopleSelectList.json',
  'prefix' => '',
  'plugin' => NULL,
)
Request URL: /Subaru/purchases/get-sales-people-select-list.json/47
Stack Trace:
#0 C:\WebServer\cake\subaru\vendor\cakephp\cakephp\src\Routing\Dispatcher.php(114): Cake\Controller\Controller->invokeAction()
#1 C:\WebServer\cake\subaru\vendor\cakephp\cakephp\src\Routing\Dispatcher.php(87): Cake\Routing\Dispatcher->_invoke(Object(App\Controller\PurchasesController))
#2 C:\WebServer\webdocs\Subaru\index.php(42): Cake\Routing\Dispatcher->dispatch(Object(Cake\Network\Request), Object(Cake\Network\Response))
#3 {main}

What have I missed?

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to a topic in the Google Groups "CakePHP" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cake-php/31AlwVDJmv8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: