1

This is my .ctp file ajax function => this is my ajax function in which I'm assigning the id so that I can get all the order nos for this particular id and append it to the select option.

function getOrderNum(id){
            $.ajax({
                type:"get",
                url: "/CustomerProblems/getOrders",                  
                data:"id="+id,
                // dataType:"json",
                success:function(response){
                    // $(".order").html("<option value="+response+">"+response+"</option>");                    
                    alert(response);
                    // console.log(response);
                }
            });
        }

This is my controller => Here I'm providing the id via the ajax function but not getting the response properly on ajax response

public function getOrders($id){
        $this->render(false);
        if($this->request->is('get')){
            $order_no = $this->CustomerProblems->Orders->find('all')->where(['Orders.customer_id'=>$id])->extract('order_no');
            $this->set("order_no", $order_no);    
            // pr($customers->toArray());die;
            echo json_encode($customers);
            // echo $customers;
            // die;
        }
    }
Abbas Ali
  • 11
  • 2
  • First things first, **do not echo data in controllers!** **https://stackoverflow.com/questions/42378793/how-to-output-custom-http-body-contents-with-cakephp-3-4-echoing-causes-unable/42379581#42379581** Fix that, and then do some debugging, start with inspecting your browser's network console to figure what exactly your request _does_ receive in return. Also check your log files for possible errors. – ndm Jun 09 '20 at 11:48

1 Answers1

0

As you are using get method you can try passing object parameter in the data and also try to set the content-type

function getOrderNum(id){
            $.ajax({
                type:"get",
                url: "/CustomerProblems/getOrders",                  
                data:JSON.stringify({id:id}),
                contentType: "application/json; charset=utf-8",
                success:function(response){
                    // $(".order").html("<option value="+response+">"+response+"</option>");                    
                    alert(response);
                    // console.log(response);
                }
            });
  }
prathameshk73
  • 1,048
  • 1
  • 5
  • 16
  • Can't access the variable name which I sent from the ajax query. I've used many method to fetch the value from variable it doesn't get the value. – Abbas Ali Jun 09 '20 at 13:05