1

I want to get json result using Jquery. Now I always got 'undefined' result. I can get the json print out using

alert(data);

But always return 'undefined' using

alert(data.first_name);

Jquery client-side code

$.post(
    "/modules/services/userlogincheck_new.php",
    {
        dataType: 'jsonp',
        action : "checkpassword",
        email : email,
        password : password
    },
    function(data) {
        alert(data.first_name);
    }
);

PHP server-side

if ($_POST['action'] == "checkpassword") {
    $query = "select * from users where email='" . $email . "'";
    $result = mysql_query($query, $forumdbcon) or die('Error, insert query failed');

    while ($row = mysql_fetch_assoc($result)) {
        if (($row['password'] == md5($password))) {
            $arr = array("response" => 1, "first_name" => $row['first_name'], "last_name" => $row['last_name'], "address" => $row['address1']);
            echo json_encode($arr);
        } else {
            $arr = array("response" => 2);
            echo json_encode($arr);
        }
    }
}
4
  • 1
    please don't edit questions' original content to reflect suggestions - it confuses people reading answers that were based on what you originally had. Commented Oct 3, 2011 at 15:51
  • 1
    since there is an else inside the php code and in that else you are not setting the first_name, are you sure it is going in the if and not in the else? Commented Oct 3, 2011 at 15:52
  • What does alert(JSON.stringify(data, null, 2)) output? Commented Oct 3, 2011 at 15:56
  • Thanks for suggestions! I'll not edit original content anymore. Commented Oct 3, 2011 at 15:57

5 Answers 5

1

Try this:

$.post("/modules/services/userlogincheck_new.php", {
   action : "checkpassword",
   email : email,
   password : password
}, function(data) {
   alert(data.first_name);
},'json');
Sign up to request clarification or add additional context in comments.

2 Comments

actually $.post is a shortcut method of $.ajax. and if you want data to be returned in your expected format other than html (which is default) then you need to specify it like 'json'. if can also use 'html' if you intended to get data in html format.
@Sophia: Take a look at the docs for $.post. The 2nd parameter is the data to send to PHP, and the 4th parameter is the dataType. So your dataType: 'jsonp' is just sending $_POST['dataType'] to PHP.
1

The response sent from the server is JSON not JSONP. Change dataType to JSON.

2 Comments

The OP's using $.post, not $.ajax, it needs to be ,'json'), not dataType: json.
You're right, the syntax is wrong. Regardless, the dataType param for the $.post function needs to be set to json.
1

"Off topic": Your code is might be highly vulnerable to SQL Injection. If I transmitted ';DROP TABLE users; -- as my email address, you woud be in trouble.

On topic:

$.post(
    "/modules/services/userlogincheck_new.php",
    {
        action : "checkpassword",
        email : email,
        password : password
    },
    function(data) {
        alert(data.first_name);
    },
    'json'
);

$.post() needs the data-type to be set explicitely.

2 Comments

It may be vulnerable to SQL injection. The OP doesn't show where they set $email. I assume they're doing $email = mysql_real_escape_string($_POST['email']);.
Updated "is" to "might be" :)
1

You don't need jsonp since this is on the server. Try changing dataType: 'jsonp' to dataType: 'json'.

You're using $.post, so you're just sending the dataType to PHP, which doesn't do anyting, you need to tell $.post to use JSON.

$.post("/modules/services/userlogincheck_new.php", {
  action : "checkpassword",
  email : email,
  password : password
}, function(data) {
  alert(data.first_name);
}, 'json;);

1 Comment

@Sophia: I misread your original code, try my updated answer.
0

You're using jsonp, so you need to wrap your output in a function call, per the parameter given by the automatically created jsonp parameter.

echo $_GET['jsonp'] . '(' . json_encode($arr) . ');';

Alternatively, just use plain JSON instead.

2 Comments

jQuery usually sends the callback parameter via $_GET.
Also, I just noticed, this isn't actually JSONP. The OP's using $.post (not $.ajax), so dataType is just being sent to PHP as a $_POST variable (which doesn't do anything).

Your Answer

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