views:

29

answers:

1

I was playing with $.getJSON in the following way to receive an alert but without luck.

Here is the code,

<script type="text/javascript">
    $(document).ready(function() {
        var url = "ticker.js?callback=?";
        $.getJSON(url, function(data) {
            alert(data.price);
        });

    });
</script>

And the ticker.js file has only the following line

{ticket:'IBM',price:14.57}

It is supposed to get an alert "14.57" but I am not getting the alert.

If you would like to see it in action you may try the link, http://nazmulweb.com/site5/demo/jsonPtest/

+1  A: 

If it's a local file, you should remove the ?callback=? part, like this:

var url = "ticker.js";
$.getJSON(url, function(data) {
   alert(data.price);
});

If you look at the $.getJSON() docs, there's this:

If the URL includes the string "callback=?" in the URL, the request is treated as JSONP instead. See the discussion of the jsonp data type in $.ajax() for more details.

With JSONP it's expecting randomFunctioName({ticket:'IBM',price:14.57}); in the response, not just the JSON, but a function call...this is how JSONP works cross-domain, by being a <script> tag. Since you just want JSON and to process it from a local source, remove the callback=? piece.

Edit: I completely missed the second issue, your current JSON isn't valid and will fail jQuery's checks added in 1.4. It needs to be this:

{ "ticket": "IBM", "price": 14.57 }

You can test your JSON for validity here: http://www.jsonlint.com/

Nick Craver
The file is in the same domain.
Hoque
@Hoque - Right...that's my point, and it's *not* JSONP which is what `callback=?` instructs it to be, so just leave it off and you're all set :)
Nick Craver
Even removing "callback=?", I did not get the alert.
Hoque
I was trying to test it at first for the same domain and I had a plan to do it in the different domain. However my first attempt did not work.
Hoque
@Hoque - There's a second part, that's not valid JSON :) It needs to be `{ "ticket": "IBM", "price": 14.57 }`, I'll update the answer.
Nick Craver
Thank you so much. After removing the callback and correcting the ticker.js file, it now gives alert in Firefox but no alert in IE8 or IE7. I have made a link here, http://nazmulweb.com/site5/demo/jsonPtest/withoutcallback.aspx
Hoque
@Hoque - I'm getting an alert in IE8, make sure to blow away your cache of the old `.js` that may be hanging around.
Nick Craver
Thank you so much. You are right. After cleaning the cache, it works perfectly. Now I will try for different domain to have a taste of jSONP.
Hoque
While I was trying JSONP with callback? option in different domain I am getting "invalid label" error. What could be the reason?
Hoque
@Hoque - It sounds like that domain doesn't support JSONP, it has to support it server-side (it returns `functioName({ JSON })` instead of just `{ JSON }` in the response...it's how it works as a GET request.
Nick Craver
Then what is the way to support JSONP in the domain I am working? Is there anything that I can do with my domain. Thank you.
Hoque
@Hoque - Nope, unfortunately nothing you can do, has to be done on the server-side, so their end.
Nick Craver