0

I've got a problem with sending json data.

Here's my php file:

<?php
session_start();
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
$result_array = array();
$result = mysql_query("SELECT Reg FROM bool");

while ($row = mysql_fetch_array($result)) 
$result_array[] = $row;


echo json_encode($result_array);
mysql_close($con);
?>

And here's my js script:

$(function () {
    $(document).ready(function() {
    $.getJSON("sql_bool.php", function(data) {
    alert(data[1]);     
    });    
});    
});

I've got some bool data stored in my database, and when i try to check with an alert() function if it made it through, all i get is an alert window with

[object Object]

Any idea what's wrong?

7
  • How do you want to convert the object to a string? Commented Nov 28, 2014 at 0:03
  • Can you do console.log(data) instead? alert is too crude. (Make sure to open the debug console; F12 in most browsers). Commented Nov 28, 2014 at 0:04
  • Do what Halcyon suggests, and also add header("Content-Type: application/json"); in your php script. Commented Nov 28, 2014 at 0:05
  • Tried console.log(data), got nothing. Commented Nov 28, 2014 at 0:05
  • Check the debug console. Commented Nov 28, 2014 at 0:07

2 Answers 2

1

It sounds like it went through fine. When you alert in JavaScript it turns whatever you alert into a string. In this case your object as a string would be [ object object].

You could try:

for(var k in data[1]){ alert(k); alert(data[1][k]); }

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

Comments

0

What you get back is a JSON encoded object. If you give that to javascript using getJSON it will handle it like an object. If you want to see the actual string you got back from the server, you could try this:

alert(JSON.stringify(data));

That should output everything you got back from the PHP script in a readable manner :)

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.