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);
        }
    }
}
vzwick
  • 11,008
  • 5
  • 43
  • 63
  • 1
    please don't edit questions' original content to reflect suggestions - it confuses people reading answers that were based on what you originally had. – Alnitak Oct 03 '11 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? – Gianpaolo Di Nino Oct 03 '11 at 15:52
  • What does `alert(JSON.stringify(data, null, 2))` output? – vzwick Oct 03 '11 at 15:56
  • Thanks for suggestions! I'll not edit original content anymore. –  Oct 03 '11 at 15:57

5 Answers5

1

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

James
  • 20,957
  • 5
  • 26
  • 41
  • The OP's using `$.post`, not `$.ajax`, it needs to be `,'json')`, not `dataType: json`. – gen_Eric Oct 03 '11 at 15:56
  • 1
    You're right, the syntax is wrong. Regardless, the dataType param for the $.post function needs to be set to json. – James Oct 03 '11 at 16:02
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;);
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
1

Try this:

$.post("/modules/services/userlogincheck_new.php", {
   action : "checkpassword",
   email : email,
   password : password
}, function(data) {
   alert(data.first_name);
},'json');
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Vikas Naranje
  • 2,350
  • 5
  • 30
  • 40
  • 1
    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. – Vikas Naranje Oct 03 '11 at 16:00
  • @Sophia: Take a look at the docs for [`$.post`](http://api.jquery.com/jQuery.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. – gen_Eric Oct 03 '11 at 16:02
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.

Community
  • 1
  • 1
vzwick
  • 11,008
  • 5
  • 43
  • 63
  • 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']);`. – gen_Eric Oct 03 '11 at 16:00
  • Updated "is" to "might be" :) – vzwick Oct 03 '11 at 16:02
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.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • jQuery usually sends the `callback` parameter via `$_GET`. – gen_Eric Oct 03 '11 at 15:48
  • 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). – gen_Eric Oct 03 '11 at 16:00