Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7565501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:10:17+00:00 2026-05-30T14:10:17+00:00

I followed this tutorial to set up my REST webservice for JSON and XML.

  • 0

I followed this tutorial to set up my REST webservice for JSON and XML. XML outputs properly but when I make a JSON call I get the view not found display from Cake.

To do this I added the following code in my AppController:

     if ( $this->RequestHandler->isAjax() ) {
        //Configure::write ( 'debug', 0 );
        $this->layout = 'ajax';
        $this->autoRender = false;      
      } elseif  ($this->RequestHandler->isXml()) {
        $this->layout = 'default';      
        //Configure::write ( 'debug', 0 );
      } elseif ($this->RequestHandler->ext == 'json') { 
        $this->RequestHandler->setContent('json','text/x-json');  
        $this->layout = 'default';      
      } elseif ($this->RequestHandler->accepts('html')) {
        $this->layout = 'frontend';     
      }

And this is an example of the code in one of my controller methods:

      if ($this->RequestHandler->isXml()) {
        $voicenote = $voicenote['Voicenote'];
        $this->set(compact('voicenote'));
      } else if ($this->RequestHandler->ext == 'json') {
        $voicenote = $voicenote['Voicenote'];
        pr($voicenote);
        echo json_encode(array('voicenote' => $voicenote));                                                                                                                                             
      } else {
        $this->set(compact('voicenote', 'tiny_list'));                                                                                                                                                  
      }

XML displays properly, it’s just JSON that’s the issue.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:10:19+00:00Added an answer on May 30, 2026 at 2:10 pm

    The issue is that auto-render is only being disabled if the request is performed via an AJAX request.
    When entering the address in a browser address bar, after the echo json_encode(); call, the controller will continue on in the render pipeline, looking for an action and layout template to output.


    I would suggest being consistent between your XML and JSON rendering, and output both through template files and not disable autoRender for AJAX requests.

    AppController:

    if  ($this->RequestHandler->isXml()) { // Allow a json request to specify XML formatting
      $this->RequestHandler->respondAs('xml'); // for setting headers
      $this->RequestHandler->renderAs($this, 'xml'); // for specifying templates for rendering
    } elseif ($this->RequestHandler->ext == 'json'|| $this->RequestHandler->isAjax()){ // 'action' ajax requests and all 'action.json' requests receive JSON
      $this->RequestHandler->respondAs('json');
      $this->RequestHandler->renderAs($this, 'json');
    }
    // Other requests will fall through to the default HTML rendering
    

    Your controller only needs to set the data, and each view template will format it as necessary:

    function action() {
      // ...
      $voicenote = $voicenote['Voicenote'];
      $this->set(compact('voicenote', 'tiny_list'));                                                                                
    }
    

    The JSON templates will be relatively simple, create as required by the error messages.
    Layout:

    <?php echo $content_for_layout; ?>
    

    JSON Action (app/views/controller/json/action.ctp)

    <?php echo json_encode(array('voicenote' => $voicenote)); ?>
    

    Check out the CakePHP RequestHandler documentation for more help

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've followed this tutorial: http://anandhansubbiah.com/blog/writing-your-first-android-application/ , but no matter what I do, and what
I've followed this otherwise excellent tutorial on getting Xen working with Ubuntu but am
I followed this tutorial to make NUnit tests debugable in Visual Studio. In the
So i followed this tutorial to set up the android facebook sdk in my
I'm new to Applescript, but I followed this handy tutorial, the first Google result
I followed the instructions in this tutorial to set up Zend AMF as a
I followed this tutorial: https://github.com/EllisLab/CodeIgniter/wiki/PDF-generation-using-dompdf However, I can't seem to set the path right.
I followed this tutorial on configuring the Rails plugin ExceptionNotifier. I know that I
I have followed this tutorial which allowed me to create a Silverlight DataGrid that
I followed along this tutorial for django's RSS and ATOM feeds and I got

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.