title: get json data back from ajax call
body: <p>my question is: How can my php script send json type data
  and received back into the success or complete function?</p>
  
  <p>I was trying to get <a href="http://anantgarg.com/2009/05/13/gmail-facebook-style-jquery-chat/" rel="nofollow noreferrer">this</a> chatfunction to work on my website
  Because it diddn't work, I created a minimized portion off the code to check
  if it had something to do with the json method.</p>
  
  <p>I only tested if I could get a sessionname back after the phpscript was proccessed
  What I get back is "undefined" instead of "johndoe".</p>
  
  <p>I have no idea what could be the problem.
  Obviously, the script has worked fine for others, if you see the comments on the creators page.</p>
  
  <p>this is my testingcode</p>
  
  <pre><code>&lt;?php
  session_start(); 
  $_SESSION['username'] = "johndoe" ;// Must be already set
  ?&gt;
  
  &lt;script type="text/javascript" src="includes/jquery.js"&gt;&lt;/script&gt;
  &lt;script language="JavaScript"&gt;
  $(document).ready(function(){
   $("#testjson").click(function(e){
   startJsonSession();
  
      return false;
      });
  
  
  function startJsonSession(){  
      $.ajax({
          url: "jsontest.php?action=startjson",
          cache: false,
          dataType: "json",
          complete: function(data) {
              username = data.username;
              alert(username);
          }
  
      });
  }
  
  
  }); 
  &lt;/script&gt;
  
  &lt;?php
  //the php script
  
  if ($_GET['action'] == "startjson") { startjsonSession(); } 
  
  
  
  function startjsonSession() {
      $items = '';
  
  
      /*if (!empty($_SESSION['openChatBoxes'])) {
          foreach ($_SESSION['openChatBoxes'] as $chatbox =&gt; $void) {
              $items .= chatBoxSession($chatbox);
          }
      }
  
  
      if ($items != '') {
          $items = substr($items, 0, -1);
      }*/
  
  header('Content-type: application/json');
  ?&gt;
  {
          "username": "&lt;?php echo $_SESSION['username'];?&gt;",
          "items": [
              &lt;?php echo $items;?&gt;
          ]
  }
  
  &lt;?php
  
  
      exit(0);
  }
  
  ?&gt;
  </code></pre>
  
  <p>thanks, Richard</p>