javascript - Jquery set HTML via ajax -


i have following span:

<span class='username'> </span> 

to populate have value php therefor use ajax:

    $('.username').html(getusername());      function getusername(){     $.ajax({         type: 'post',         url: mybaseurl + 'profiles/ajax_getusername',         datatype: 'json',         data: {          },         success: function(data){             document.write(data);         }     }) } 

now when debug see returned data (data) correct value html between span tags stay same.

what doing wrong?

little update

i have tried following:

    function getusername(){     $.ajax({         type: 'post',         url: mybaseurl + 'profiles/ajax_getusername',         datatype: 'json',         data: {          },         success: function(data){             $('.username').html('rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr');         }     }) }  getusername(); 

still there no html between tags (no text) when @ console method completed , has been executed.

answer little update

the error in ajax function forgot print actual response! thank of answers, of searching question here ajax function:

    public function ajax_getusername(){     if ($this->requesthandler->isajax())     {         $this->autolayout = false;         $this->autorender = false;         $this->layout = 'ajax';     }     print json_encode($this->currentclient['username']);  } 

do note using cakephp why there buildin methods. in remember print json_encode($this->currentclient['username']);

the logic flow of code not quite correct. asynchronous function cannot return execution have moved next statement time response received. instead, processing required on response must done in success handler. try this:

function getusername() {     $.ajax({         type: 'post',         url: mybaseurl + 'profiles/ajax_getusername',         datatype: 'json',         data: { },         success: function(data){             $('.username').html(data); // update html here         }     }) }  getusername(); 

Comments

Popular posts from this blog

design - Custom Styling Qt Quick Controls -

Unable to remove the www from url on https using .htaccess -