2

I've got a an AJAX page request that uses a php file designed to handle queries to my MySQL database. The php file works just fine, but for some reason it's not being nice with me. Here's what I have:

    function updateForm(){
        ID = $('#listings').val();
        $.ajax({
            type: "POST",
            url: 'query.php',
            data: "query=true" +
                    "&id=" + ID,
            datatype: 'json',
            success: function(data) {
                alert(data);
                updatePreview();
            }
        });
    }

gives me a popup with:

{"results":[{"ID":"12","area":"Hoboken","bedrooms":"5","fullbath":"3","halfbath":"1","remarks":"No remarks to speak of.","sqft":"2500","photos":null,"price":"1000","fee":null,"realtor":"Jane Doe","phone":"555-555-5555","address":"10th & Willow","bix":"1"}]}

but as soon as I change it to:

    function updateForm(){
        ID = $('#listings').val();
        $.ajax({
            type: "POST",
            url: 'query.php',
            data: "query=true" +
                    "&id=" + ID,
            datatype: 'json',
            success: function(data) {
                alert(data.results);
                updatePreview();
            }
        });
    }

the popup just says undefined.

Ultimately, I want to parse out information and update my page accordingly, but I can't seem to access any of the properties of this JSON object. What's going on?

EDIT:

Here's the code from the php file:

if (isset($_POST['query'])){
    if (isset($_POST['id'])){
        $query = 'SELECT * FROM bix WHERE ID=' . get_post('id');
        $listing = mysql_query($query);
        print_json($listing);
    }
}
function print_json($var){
    $output = array();
    while($row = mysql_fetch_assoc($var)) {
        $output["results"][] = $row;
    }
    echo json_encode($output);
}

function get_post($var)
{
    return mysql_real_escape_string($_POST[$var]);
}
3
  • can you post your server side code? Commented Mar 27, 2012 at 21:18
  • also if you console.log() both what do you get? Commented Mar 27, 2012 at 21:18
  • maybe you didn't capitalize the T in dataType... Commented Mar 27, 2012 at 21:37

6 Answers 6

2

If it shows you a string in the alert popup it means that the data is a string, not JSON. For JSON it shows [Object object].

Check the response type. It should be applicaton/json or applicaton/javascript, not text/plain or something like that.

UPDATE: also make sure the server doesn't return the whole string in quotes. E.g. "[..]" - it just a string then. So use FireBug or Chrome Developer Tools to see what is the actual response. It might help to understand why jQuery doesn't handle it properly.

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

1 Comment

This is interesting however according to the jQuery docs a datatype: 'json', is supposed to do the lifting for you... can you post your server side code?
1

did you try capitalizing the T in dataType, on the ajax call already?

Comments

1

You should console.log(data) instead, with this you can see the keys that you can use to access the data.

Comments

1

I don't think it's coming back as parsed json..it's coming back as a string I believe...

use:

var x = jQuery.parseJSON(data);
alert(x.results)

5 Comments

It should, though. The documentation says that datatype: 'json' returns a JSON object, not a string.
did you try capitalizing the T in dataType, on the ajax call already?
Ahhh, that's totally what the problem was.
If you write that in as a response I'll give you the check mark!
I added a new response. you can check that. glad that fixed it.
0

Try using data instead of data.results or targeting the data.id or data.area and see what it comes up with

Comments

0

As Eugene pointed out, data is a string. Try eval:

var stuff = eval(data);
alert(stuff.results);

EDIT: this works, but jQuery.parseJSON is probably cooler.

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.