0

I am trying to access the session variables through a Jquery Ajax call. However, the jquery response keeps throwing the "Unexpected token < in JSON" error.

Here is my Jquery code -

$( document ).ready(function() {
$.ajax({
        url: "mysession.php",
        dataType: "json",

        success: function(response){
            if(response.userid == ""){
                alert(response.userid);
                $("#WelcomeMsg").html("");
            }else{
                $("#WelcomeMsg").html("Welcome<br>"+response.username);
            }

        },
        error : function(jqXHR, textStatus, errorThrown) {
                    alert(jqXHR.status+",  " + jqXHR.statusText+",  "+textStatus+",  "+errorThrown);
        }
    });

  });

On the PHP side my code includes the following -

header("Content-Type: application/json");

// Start the session
session_start();
if(isset($_SESSION["user"]))
{
    $userid = $_SESSION["user"];
    $username = $_SESSION["name"];
    $studentlog = array("userid" => $userid , "username" => $username, "Err"=>"");
    echo json_encode($studentlog);
}else
{
    $userid = "";
    $username = "";
    $studentlog = array("userid" => $userid , "username" => $username, "Err"=>"");
    echo json_encode($studentlog);
}

Can somebody please suggest what wrong I am doing while doing the json encoding

Edit: A little more background to the problem. As you can understand I am basically new to PHP and using session variables for the first time. Earlier I had written a login script with similar code (but did not use session variables), to get username from the database and perform user validation. The Jquery Ajax calls were absolutely similar and the data was being returned by json_encode. The code seemed to have been working fine.

But now after I have started using session variables to get the user login status there seems to be a problem. In fact the same login script is also throwing an error. (I have provided the other php script over here considering the length of the login script)

One more observation, if I simply comment out session_start() in the login script, everything once again works fine. Hope I have explained myself fine.

soumyajyoti
  • 79
  • 1
  • 13
  • 1
    Have you tried printing out the response value, to make sure it contains valid JSON? – Traveling Tech Guy May 31 '16 at 01:53
  • most probably you get an error and what you get as a response is an html, then jquery is trying to parse it as json - with the error you had shown – Jerzyk May 31 '16 at 02:17

1 Answers1

2

Edit

Based on your new issue, you're outputting data somewhere. Are you starting your script with <? instead of <?php as that might be the issue. The other thing I can think of is that you're already sending headers (Read more about this issue/error).

I'll need you to turn on error reporting at the top of your script and post that errors here to me.

<?php
ini_set('display_errors', 1);
error_reporting(-1);

The issue is in your PHP. It's probably throwing an error or outputting HTML instead of JSON that you're requesting.

We'll need to see that code, but in the mean time you can insure that your PHP is returning the proper JSON Content Type by specifying it as such via the headers:

<?php
header("Content-Type: application/json");

Looking at your PHP code, there are 2 things that pop up.

The global variable you want is $_SESSION, not $_Session. You'll be getting an "Undefined Variable" notice due to that.

What version of PHP are you using? You're using the short array syntax - [..] which was introduced in PHP 5.4.

You could try changing that to:

$studentlog = array("userid" => $userid , "username" => $username, "Err"=>"");

You could also shorten your PHP using ternary operators:

<?php
session_start();
header("Content-Type: application/json");

$userid = ($_SESSION['user']) ? $_SESSION['user'] : "";
$username = ($_SESSION['name']) ? $_SESSION['name'] : "";
$studentlog = ["userid" => $userid , "username" => $username, "Err"=>""];
echo json_encode($studentlog);
Community
  • 1
  • 1
Darren
  • 13,050
  • 4
  • 41
  • 79