如何在ajax响应中获取数组到另一个字段

时间:2018-12-02 02:53:40

标签: php ajax codeigniter codeigniter-3

我有一个类似的数组

Router Event: NavigationStart
NavigationStart(id: 2, url: '/Account/Profile')
NavigationStart {id: 2, url: "/Account/Profile", navigationTrigger: "imperative", restoredState: null}

Router Event: RoutesRecognized
RoutesRecognized(id: 2, url: '/Account/Profile', urlAfterRedirects: '/Account/Profile', state: Route(url:'', path:'') { Route(url:'Account', path:'Account') { Route(url:'Profile', path:'Profile') }  } )
RoutesRecognized {id: 2, url: "/Account/Profile", urlAfterRedirects: "/Account/Profile", state: RouterStateSnapshot}

Router Event: GuardsCheckStart
GuardsCheckStart(id: 2, url: '/Account/Profile', urlAfterRedirects: '/Account/Profile', state: Route(url:'', path:'') { Route(url:'Account', path:'Account') { Route(url:'Profile', path:'Profile') }  } )
GuardsCheckStart {id: 2, url: "/Account/Profile", urlAfterRedirects: "/Account/Profile", state: RouterStateSnapshot}

Router Event: ChildActivationStart
ChildActivationStart(path: '')
ChildActivationStart {snapshot: ActivatedRouteSnapshot}

Router Event: ActivationStart
ActivationStart(path: 'Profile')
ActivationStart {snapshot: ActivatedRouteSnapshot}

Router Event: GuardsCheckEnd
GuardsCheckEnd(id: 2, url: '/Account/Profile', urlAfterRedirects: '/Account/Profile', state: Route(url:'', path:'') { Route(url:'Account', path:'Account') { Route(url:'Profile', path:'Profile') }  } , shouldActivate: true)
GuardsCheckEnd {id: 2, url: "/Account/Profile", urlAfterRedirects: "/Account/Profile", state: RouterStateSnapshot, shouldActivate: true}

Router Event: ResolveStart
ResolveStart(id: 2, url: '/Account/Profile', urlAfterRedirects: '/Account/Profile', state: Route(url:'', path:'') { Route(url:'Account', path:'Account') { Route(url:'Profile', path:'Profile') }  } )
ResolveStart {id: 2, url: "/Account/Profile", urlAfterRedirects: "/Account/Profile", state: RouterStateSnapshot}

和我的ajax

ResolveEnd

控制器:

[{"id_pelanggan":"1","id_tipe":"5","nama_pelanggan":"ASD","alamat_jalan":"DS","alamat_kota":"1","alamat_provinsi":"ATA","kontak_pelanggan":"45454"}]

型号:

$(function() {
  $("#autocomplete").change(function(){
    var namaagen = $("#autocomplete").val();
    $.ajax({
      url: '<?php echo site_url('Pelanggan/tampil_where'); ?>',
      type: 'POST',
      dataType: 'json',
      data: {
        'namaagen': namaagen
      },
      success: function (agen) {
        $("#napem").val(agen['nama_pelanggan']);
      }
    });
  });
});
  });
});

但没有任何反应,我已经添加了public function tampil_where(){ $nama = $this->input->post('namaagen'); $query = $this->pelanggan_m->tampilwhere($nama); echo json_encode($query);} 作为响应成功,并显示了警报未定义的pls帮助,谢谢您的帮助

1 个答案:

答案 0 :(得分:0)

请更改您的警报代码,现在您无法以正确的方式访问响应数组:-

alert(agen[0]['nama_pelanggan']);

而不是:-

alert(agen['nama_pelanggan']);

谢谢