0
votes

I am using CakePHP 2.9 to send data on the URL using ajax and get the related response.

I tried may method to get the response, I also want to know why this //URL:'/Pages/dropdownbox/'+id is not working.

bellow are ajax code which I wrote in the index.ctp.

$("#certificatedetail").on('change',function() {
    var id = 'subcribe';
    $("#usertype").find('option').remove();
    $("#certificateclass").find('option').remove();
    $("#certificatetyp").find('option').remove();

    if (id) {
        $.ajax({
            type: 'POST',
            url:'<?= Router::url(array('controller' => 'Pages', 'action' => 'dropdownbox','id')); ?>',
            //url:'/Pages/dropdownbox/'+id,
            dataType:'json',
            cache: false,
            async:true,
            success: function(html) 
            {
                $('<option>').val('').text('select').appendTo($("#usertype"));
                $('<option>').val('').text('select').appendTo($("#certificateclass"));
                $('<option>').val('').text('Select').appendTo($("#certificatetyp"));

                $.each(html, function(key, value) 
                {
                    $('<option>').val(key).text(value).appendTo($("#usertype"));
                });
            }
        });
    }
});

I have written this controller code in PagesController,PHP and I declared the dropdownbox in AppController.php

public function dropdownbox($id = null)
{
    Configure::write('debug', 0);
    $this->layout = null;
    $this->autoRender = false;
    $category = array();

    switch ($id)
    {
        case $id == "subcribe":
            if ($id == 'subcribe') {
                $category = array(
                    'individual' => 'Individual',
                    'organization'=>'Organization',
                    'organizationgovt' => 'Organization-Govt',
                    'organizationbank' => 'Organization-Bank'
                );
                break;
            }
    }
}

/ bellow is the code where I specify the dropdownbox function in AppController.php

public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow(
        'login','add','index','contact','dropdownbox',
        'cityres','stateres','sectorres','productres',
        'project','service','about','apply','tender',
        'decregistration','search','searchresult',
        'tenderdetails'
    );
}
2
Your dropdownbox() method doesn't generate any response, you've disabled auto rendering and you're not invoking rendering (Controller::render()) manually, so "not getting any response" would be the expected behavior. Furthermore you cannot add methods to the default PagesController that ships with the default CakePHP app skeleton, unless you create routes that allow connecting to these actions. - ndm

2 Answers

0
votes

You are generating the URL on your server, and using the string literal 'id' in it. The JavaScript id variable is never referenced. What you probably want is:

url:'<?= Router::url(array('controller' => 'Pages', 'action' => 'dropdownbox')); ?>' + id,
0
votes

You are not returning any response from the controller. Do few things to debug it

  1. Check in Browser's Network tab whether the called URL is correct or not.
  2. Check the parameters are correct or not.

enter image description here This is how it looks in Firefox Developer Edition

  1. If URL is correct. Add below code in the dropdownbox() method. (Before the closing of the method)

    echo json_encode($category);

  2. Check Response Tab in the Network tab. (Example image above).

  3. Also, console the response in the javascript code. Maybe you will be getting some other response which is not JSON.

    success: function(html) { console.log(html); ... }

Hope it helps.