AJAX returns undefined variable after called : <p>I´m developing a session system with Jquery, AJAX, PHP and JSON.</p>

<p>This is my generic AJAX function:</p>

<pre><code>function ajaxConn(incomingUrl,incomingData)
  {
    var returnValue;
    $.ajax({
      url:'backend/'+incomingUrl,
      method:'POST',
      data: JSON.stringify(incomingData),
      contentType: "application/json; charset=UTF-8"
    }).done(function(response){
          returnValue = response.resp;
    });
    console.log(returnValue);
  }
</code></pre>

<p>This is the call of the function and the storage of result to be used. variable data has inputs values previously filtered and validated.</p>

<pre><code>var data = {user:user,pass:pass};
var answer = ajaxConn('core_files/startSession.php',data);
console.log(answer);
</code></pre>

<p>This is the headers of my PHP code</p>

<pre><code>require 'connection.php';
mysqli_set_charset($conn,"utf8");
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
$data = json_decode(file_get_contents('php://input'), true);
$user =  mysqli_real_escape_string($conn,$data["user"]);
$pass =  mysqli_real_escape_string($conn,$data["pass"]);

$response = array();
</code></pre>

<p>After a lot of validations PHP returns a word depending of the session case:
It can returns </p>

<pre><code>$response["resp"] = 'N';
echo json_encode($response); 
</code></pre>

<p>for wrong session or</p>

<pre><code>$response["resp"] = 'Y';
echo json_encode($response); 
</code></pre>

<p>for success session.</p>

<p>The problem is coming on the value return, if I do <code>console.log(returnValue)</code> inside the AJAX call, it returns the correct word from PHP. But, if I exit from AJAX call and I try to do the same out the funcion it returns me undefined. Actually, in the return of the function it returns me undefined too, outside the </p>

<pre><code>ajaxConn();
</code></pre>

<p>What I'm doing wrong</p>
