retrieve data from controller in json code igniter

2019-06-12 15:42发布

问题:

    $(document).ready(function(){

        $('#bill_no').blur(function(){

            if( $('#bill_no').val().length >= 3 )
                {
                  var bill_no = $('#bill_no').val();
                  getResult(bill_no); 
                }
            return false;
        })
        function getResult(billno){
            var baseurl = $('.hiddenUrl').val();
          //  $('.checkUser').addClass('preloader');
            $.ajax({
                url : baseurl + 'returnFromCustomer_Controller/checkBillNo/' +   billno,
                cache : false,
                dataType: 'json',
                success : function(response){
                    $(".text").prepend(response.text);
                }
            })
        }
    })

my controller

         function checkBillNo($billno){
    $this->load->model('returnModel');
    $query = $this->returnModel->checkBillNo($billno);



        header('Content-Type: application/x-json; charset=utf-8');
       echo(json_encode($this->returnModel->sale($billno)));


}

how can i print the value in span class "text" after getting the values from controller.. i have checked in firebug in which in response tab i am successfully getting my result but how can i print in my view page in span class ..

回答1:

you need to get the response as objet.parameter like this:

success : function(response)
 {
    $(".text").html(response.result);
 }

Because as you said in your comment:

this is the response {"result":"142"}



回答2:

 success : function(response)
 {
    $(".text").html(response);
 }


回答3:

You can use segment no to retrieve the parameter from url

function checkBillNo($billno)
{
    $this->load->model('returnModel');

    $query = $this->returnModel->checkBillNo($billno);

    $billno =   $this->uri->segment(3);
    $billno_results  = $this->returnModel->sale($billno)

    //header('Content-Type: application/x-json; charset=utf-8');
    echo    json_encode($billno_results);
}

What is the use of $query here. Also you dont need to set header type

ANd your ajax here

$.ajax({
    url : baseurl + 'returnFromCustomer_Controller/checkBillNo/' +   billno,
    cache : false,
    dataType: 'json',
    success : function(response){
        $(".text").prepend(response);
    }
})

See you dont need response.text simple print response