2

Please somebody help me, am new to ajax, i have been trying to read json data from php script but just no success. when i console.log the data i get this,

{"name":"JOHN","surname":"FIGO","sex":"M","Records_id":"1","student":""}.

and when i do this console.log(data[2]); i simply get n character. what i want is to get the values, for example, console.log(data['name']); should give JOHNor console.log(data[0]); should give JOHN. when i use either javascript or jquery parse methods, i get an error, i dont understand. Here is are the codes;

<?php 
$conn= new mysqli('localhost', 'root', '', 'Goldfinger');

$query= 'SELECT * FROM records';

$result= $conn->query($query);

     while($row = $result->fetch_assoc()) {
       echo json_encode($row);

}
?>

and jquery;

$('#one').click(function () {

        $.ajax({
            url: 'ajaxtesting.php',
            type: 'POST',       
            success: function (data) {
                if (data) {
                    console.log(data['name']);
                };
            }, 
            error: function () {
                $('div:not(#one)').text('error dude!');
            }
        })
    })

pardon my code if it's very poor. Thank you in advance.

2
  • 1
    use console.log(data.name) console.log(data.surname) Commented Oct 1, 2015 at 8:18
  • am getting undefined! Commented Oct 1, 2015 at 8:39

1 Answer 1

3

Put dataType : 'json', inside ajax setting like so :

 $.ajax({
        url: 'ajaxtesting.php',
        type: 'POST',
        dataType : 'json', //<------------- here   
        success: function (data) {
            if (data) {
                console.log(data['name']);
            };
        }, 
        error: function () {
            $('div:not(#one)').text('error dude!');
        }
    })

or simply parse inside success callback :

 $.ajax({
        url: 'ajaxtesting.php',
        type: 'POST',       
        success: function (data) {
           var myData = $.parseJSON( data ); //<------- here
            if ( myData ) {
                console.log(myData['name']);
            };
        }, 
        error: function () {
            $('div:not(#one)').text('error dude!');
        }
    })
Sign up to request clarification or add additional context in comments.

6 Comments

by default i think ajax returns a JSON if you dont specify it.Correct me if I am wrong. Thank you
If no dataType was specified, jQuery will try to refer based on the MIME type of the response, otherwise string will returned. It common practice to specify the dataType other than string.
I see i thought the default is JSON if you dont specify anything thank you for clarification. I hope you explain this in the answer and also provide a documentation for better understating of the OP as well
for your first method i get the error, while the second gives, Uncaught SyntaxError: Unexpected token {, ajax.html1.
I guess something wrong with echo json_encode($row);, why you put inside loop anyway?
|

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.