0

I am trying to fetch an object from json and it is showing undefined. Can i get help please. below is my code.Instead of getting the name from the database it shows undefined. i think the problem is in my success function. please help me out.

<script>
$(document).ready(function(){
    $.ajax({
        type: 'GET',
        url: 'connections/profile.php',
        data: 'param=no' ,
        ataType: "html",
        success: function (response) {
            console.log(response);
                $('#basicContent').html('<h4><b>' + response.name + '</b></h4>');

        },
        error: function (e){
            alert (e);
        }

    });
});
</script>

also my php

<?php
require_once('connect.php');
session_start();
if (!isset ($_SESSION['matric']))
{
$go="index.html";
header("Location:".$go);
}

$matric = $_SESSION['matric'];
$pass= $dbh->prepare("SELECT * FROM users WHERE matric=:matric");
$pass->bindParam(':matric', $matric);
$pass->execute();
$profile=$pass->fetch(PDO::FETCH_ASSOC);
$response = array(
'name' => $profile['name'],
'matric' => $matric,
'school' => $profile['school']
);
echo json_encode($response);
1
  • Could we see an example of some JSON that is decoded as undefined? Commented Jul 10, 2015 at 0:42

1 Answer 1

1

Change dataType from html to JSON

Also in the php file add a header to serve JSON content

header('Content-Type: application/json');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.