1

I don't know much about JSON and have never been able to return something with JSON into ajax and show it using jquery despite of my many tries.
What I am trying to do is, send data using JSON object to the ajax while loading the profile of a user.

[Updated] php code:

<?php include(dirname(__FILE__). '/../script/config.php');
session_start();
$id = $_POST['u_search'];
$email = $_SESSION['Email'];

foreach($pdo->query("SELECT * FROM Users WHERE ID='$id'") as $row) {
    //$firstname = $row['FirstName'];
    //$lastname = $row['LastName'];
    $pic = $row['Pic'];
    $id = $row['ID'];
    $u_email = $row['Email'];
}
$firstname = "Jason";
    $lastname = "Born";
    $data = array("success"=> true,"inpt"=>"<p>Hello there! I am " . $firstname . " " . $lastname . "</p>");
    echo json_encode($data);
header("Content-Type: application/json");)
?>

<?php $pdo = null; ?>

Updated Ajax:

function op_prof(obj) {
    var value = obj.id;
    var dataString = "{'u_search':'"+value+"'}";
    $("#co_profile").show();
    $(".searchbox").val('');
    $("#usr_suggest").hide();

    $.ajax({
    type: "POST",
    url: '/script/profile.php',
    dataType: 'json',
    data: dataString,
    cache: false,
    success: function(data) {
        alert(console.log(data));
        alert(data);
        $("#co_profile").html(data.inpt).show();
        location.hash = 'profile' + 'id=' + dataString;
    }
  });
};

edit: When I use dataType: 'json' , nothing in success runs but when I remove it, they run..

edit: When I use datatype: 'json' instead of dataType: 'json' the codes in success run. I used alert(console.log(data)); , it says "undefined"

edit: I am using //ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js

27
  • First try to console.log(data) in success and print log in your question. Commented May 17, 2013 at 5:22
  • I am able to load json data Properly in my example with your preference. If you want I can share it. But first show your console.log(data). Commented May 17, 2013 at 5:33
  • Nothing in success runs .. thats wired .. I have put console.log(data); in success Commented May 17, 2013 at 5:33
  • But when I remove dataType: 'json' , everything in success runs Commented May 17, 2013 at 5:38
  • If you run your profile.php file then does it shows any error? Because I have doubt on use of '$id' and '$pic' in your first condition. Commented May 17, 2013 at 5:50

3 Answers 3

1

if you put datatype:json means you have to send data:"" in json string format and even in the server side the variable name should match, so that it can read the value which you have sent.

example:

datatype:'json',
data : JSON.stringify({'u_search':'value'})

used JSON.js file for converting object to string.

Sign up to request clarification or add additional context in comments.

Comments

1
var dataString = "{'u_search':'"+value+"'}";

dataType: 'json',
data: dataString,

Edit:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

Comments

0

Try this.

$.post("/script/profile.php",{dataString:dataString},function(data){
                alert(console.log(data));
    alert(data);
    $("#co_profile").html(data.inpt).show();
    location.hash = 'profile' + 'id=' + dataString;
            }, "json");   // USing JSON here for the returned value from server...

In your profile.php file you must be decoding the json format. Remember to collect the decoded json value in a variable with name dataString

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.