0

I have a table of categories and subcategories that I need, when the product is added and the user selects a category of my choice, the following list of subcategories should display only those subcategories that have ... I use ajah in my admint.ctp everything seems fine but my controller does not receive ajah request

$('#category_id').change(function(){
        $('.typecategories-select').fadeIn('slow');
        var category_data = $(this).val()
        console.log(category_data)
        $.ajax({
            type: "POST",
            url: '<?php echo $this->Url->build(['controller' => 'Products', 'action' => 'add','plugin'=>'admin']); ?>',
            data: {category_id:category_data},
            success: function(data){
                console.log('AjaX Success')
                // console.log(data)
            },
            error: function(){alert('AjaX Failed')}
        });


    });
    
    }
    
    
    
    
    //ProductsController
public function add()
    {
    if ($this->request->is('post')){
            $a = 'ajax success';
            echo $a;
        }
        if ($this->request->is('put')){
            $a = 'ajax success';
            echo $a;
        }
        if ($this->request->is('ajax')) {
            $a = 'ajax success';
            echo $a;
        }
        else{
            echo $value = $this->request->getData('category_id');
            echo $value;
            $a = 'a';
            echo $a;
//here only return $a after i change #category_id but its ajax request wtf?

        }
        if ($this->request->is('json')){
            $a = 'ajax success';
            echo $a;
        }
        
}        
  • Check the URL that Cake is building for you from the provided array. Make sure it looks like what you're expecting, and that it "works" when you try to access it directly in your browser instead of through Ajax. – Greg Schmidt Nov 08 '21 at 15:22
  • @GregSchmidt can you explain in a little more detail? i am making this request from my layout with admin plugin – Вербуховський Кирил Nov 09 '21 at 11:08
  • "View source" on the page that has the code for the Ajax call in it. What is the URL that it has created for you there? What URL are you expecting there? – Greg Schmidt Nov 09 '21 at 14:28
  • 1
    Please debug your code properly, put an exit statement after "echo", also debug the request: you will know the exact request method there. also inspect the browser's network tab->xhr request : there you will know what parameters, headers etc are sent. – D Coder Nov 09 '21 at 17:09

0 Answers0