0
$(document).ready(function() {
    $.post('matchEngine.php',
        function(data){
        $('#results').html(data);

        alert("data gotten!");
    }, 'json');
});

the alert appears and I can put dummy text appear in the html eg $('#results').html("very annoying");

the php echoes a json_encode array if I just go to the page, so what am I doing wrong??

2
  • 1
    try $('#results').html(JSON.stringify(data)); Commented Jun 6, 2013 at 18:04
  • @mikeB cheers also got OUTPUT visable. Commented Jun 6, 2013 at 18:53

1 Answer 1

2

In your callback, data will be the deserialized object, not the JSON string. So when you pass that into html, you're basically calling toString on it and passing the result. If data is an array, that does an Array#join by default. If it's an object, toString will give you [object Object].

You should be seeing something in the results element, but the main point here is that if your goal is to work with the data in data, it's already been decoded for you.

If you want to see the actual JSON text that was returned by the script, you can do that by telling jQuery not to deserialize it for you, and by making sure when you dump it out to the HTML that any HTML entities or special characters are handled properly, like this:

$(document).ready(function() {
    $.post('matchEngine.php',
        function(data){
        $('#results').text(data); // <== `text` instead of `html`
                                  //     makes sure HTML characters
                                  //     like < are shown correctly

        alert("data gotten!");
    }, 'text');                   // <== data type 'text' instead of
                                  //     'json' tells jQuery NOT to decode
                                  //     it for you
});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.