1

This is my PHP page (on a different URL)...

<?php
header('Content-Type: application/json');
?>
stat({"online":1});

And this is my jQuery:

$(document).ready(function(){

    var url = 'http://blah.com/jsontest.php?callback=stat';

    $.getJSON(url, function(data) {
        if (data.online !== undefined) {
            console.log('yay');
        }
    }).error(function() {
        console.log('no');
    });

});

For some reason it is always logging 'no' i.e. its running the error function.

Using jQuery 1.5.2 any ideas?

6
  • If you console.log(data) what do you get? Commented May 5, 2011 at 11:37
  • Is your getJSON call recognized as a JSONP one? Commented May 5, 2011 at 11:38
  • 1
    @Nicola — nothing, because the error function is being called not the success function. Commented May 5, 2011 at 11:41
  • @David - i also don't understand the "stat({"online":1});" part in the server side script. It might be obvious, but usually i echo('{"online":1}');die(); on server side to have things work P.S. ok, my first comment was really stupiud, sorry! :) Commented May 5, 2011 at 11:43
  • I believe that if you want to name your own callback function you need to use $.ajax() and specify the jsonpCallback in the call setup. Commented May 5, 2011 at 11:45

2 Answers 2

4

First, JSON-P is not JSON. The content type should be application/javascript. Some browsers may reject JSON-P served as JSON for being unsafe.

Second, getJSON expects that the URL you request to have a ? for the callback method name (and you'll need to get your PHP to pay attention to $_GET['callback']).

Third, if fixing that doesn't work, look at the Net tab in Firebug / Chrome debugger / Dragonfly / etc and see what data is actually going across the wire.

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

3 Comments

Can I use JSON for cross domain or do I have to use JSON-P?
You'll have to use JSONP, that's why it was ‘invented’ in the first place.
+1 for content type You really should use the built in callback=? and read the value on the server. If you use your own hard coded callback, you will most likely run into problems if there are multiple requests open. So the second point here is valid.
0

There's some shenanigans with having with include a "callback" function. Apparently you're not returning an object, but a function that was submitted in the original client request. I only vaguely understand what all that means, however, I do have some code to do this that actually works:

Server Side:

<?php
$headers = get_headers($toGetUrl,1);
$return["pop_singer"] = "Britney Spears";
// Right here is where the json object gets wrapped in a function that was submitted under the name "callback"
echo $_GET['callback']."(".json_encode($return).")";
?>

Client side ( this is the same thing as $.getJSON() ):

$.ajax({
type: "GET",
url: serverUrl,
dataType: 'jsonp',
error: function(request, status) {
    // Do some error stuff
},
success: function(data, textStatus, jqXHR) {
    var property = data.pop_singer;   // equals "Britney Spears"
    // Do some successful stuff
}

});

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.