2017-01-27 6 views
-1

Derzeit verwende ich Json, um automatisch meine reuslt, aber ich habe Fehler sagen json_encode(): Typ ist nicht unterstützt, codiert als null, und ich denke, mein Problem ist Ansicht, wenn ich im Namen wähle in Drop-Down gibt i einen Fehler json_encode(): Typ wird nicht unterstützt, codiertAuto-Anzeige Ergebnisse mit JSON

Modell

function get_address($name) { 
    $vendres = array('name' => $name); 
    $this->db->select('address'); 
    $this->db->where($vendres); 
    $this->db->distinct(); 
    $result = $this->db->get('profile'); 
    if($result->num_rows() > 0){ 
     foreach($result->result_array() as $row){ 
      echo $row['address']; 
     } 
    } 
    return $result; 
} 

-Controller

function address() { 
    $name=$this->input->post('name'); 
    $this->load->model('default/M_profile'); 
    $data['address'] = $this->M_vendor->get_address($name); 
    $this->output->set_output(json_encode($data)); 
    //echo $data; 
    return; 
} 

im Hinblick verwenden i Dropdown-Menü.

$(document).ready(function() { 
    $('#profile select').change(function() { 
     var add = $(this).text(); 
     $.ajax({ 
      url: "<?php echo base_url();?>admin/profile/address", 
      method: "POST", 
      data: {profile: add}, 

      success: function(add) { 

       $('#address').val(add); 
      } 
     }) 
    }); 
}); 
<select name="test">....</select> 

Antwort

1

Rückkehr Sie haben eine Menge Fehler:

1.In Ajax sollte type:'POST' .Nicht method:'POST' sein.

2.In Controller sollte es $this->input->post('profile')

3.In Modell sein nur Ihre Daten zurückgeben result_array() verwenden.

MODELL:

function get_address($name) { 
    $vendres = array('name' => $name); 
    $this->db->select('address'); 
    $this->db->where($vendres); 
    $this->db->distinct(); 
    $result = $this->db->get('profile'); 
    if($result->num_rows() > 0){ 
     return $result->result_array(); 
     } 
    } 

} 

Controller:

function address() { 
    $name=$this->input->post('profile'); 
    $this->load->model('default/M_profile'); 
    $data = $this->M_vendor->get_address($name); 
    echo json_encode($data); 
} 

Ausblick: (Ajax):

<script type="text/javascript"> 
$(document).ready(function() { 
    $('#profile select').change(function() { 
     var add = $(this).text(); 
     $.ajax({ 
      url: "<?php echo base_url('admin/profile/address');?>", 
      type: "POST", 
      data: {profile: add}, 

      success: function(add) { 
       var data = JSON.parse(add);//parse response to convert into onject 
       console.log(data);//see your result in console 
       alert(data[0].address); 

      } 
     }) 
    }); 
}); 
</script> 
<select name="test">....</select> 

Ich hoffe, es hilft Ihnen viel.

+0

Dies ist eine gute Antwort, ich würde empfehlen, die Zeile hinzuzufügen: header ('Content-Type: application/json'); Kurz vor dem "echo json_encode ..." in der Controller-Methode. Dies erzwingt nur den Inhaltstyp, der von der Methode zurückgegeben wird. Ich hatte zuvor Probleme mit IIS. –

+0

Ich habe ein bisschen in meinen Codes geändert, danke, dass es mir hilft. – noname

0

Sie das Ergebnis hallen statt

function get_address($name) { 
     $vendres = array('name' => $name); 
     $this->db->select('address'); 
     $this->db->where($vendres); 
     $this->db->distinct(); 
     $result = $this->db->get('profile'); 
     if($result->num_rows() > 0){ 
      foreach($result->result_array() as $row){ 
       $result[] = $row['address']; 
      } 
     } 
     return $result; 
    } 
Verwandte Themen