提问者:小点点

我想从我通过ajax函数提供的id中获取订单号,但不显示success:function(response)中的值


这是我的。ctp文件ajax函数=>; 这是我的ajax函数,我在该函数中分配id,以便获得这个特定id的所有订单号,并将其追加到select选项中。

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);
                }
            });
        }

这是我的控制器=>; 这里,我通过ajax函数提供id,但没有在ajax响应上正确地获得响应

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;
        }
    }

共1个答案

匿名用户

在使用get方法时,可以尝试在数据中传递对象参数,并尝试设置内容类型

null

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);
                }
            });
  }