(I used “jsoncallback” here, instead of just “callback” did this a few years ago, I don’t remember why…
(I tried using just “callback” on this flickr page… but then nothing prints… (but I don’t get error… oh brother… I love JSON…)
thank you…
PS: sorry, I need to change this… not getting error anymore, but nothing prints… (oh man…)
the json url is correct as I can load it directly on the browser and see the content…
this is my code now…
$.getJSON("http://dummySubdomain.domain.com/movies.json?callback", function(json) {
console.log(json);
$('div').html('json: ' + json); // this line does not get executed at all, nothing prints (neither does the console.log() but I don't get any errors)
});
$.getJSON(“http://dummySubdomain.domain.com/movies.json?callback[B]=?[/B]”, function(json) {
console.log(json);
$(‘div’).html('json: ’ + json); // this line does not get executed at all, nothing prints (neither does the console.log() but I don’t get any errors)
});
(added ‘=?’ after ‘callback’ (wanted to highlight this, but can’t find how to change font color here…)
What is that variable called value? Should it be data?
$.each(value, function (i, val) {
You’re closing off $.each for both value and for data - when you only open it fr one of them. One of these two is superfluous and is messing up your structure.
You’re closing off $.each for both value and for data - when you only open it fr one of them. One of these two is superfluous and is messing up your structure.
That doesn’t seem to be valid JSON code - what should happen instead is that a URL is called with ?callback=someFunctionName, and that callback name is then wrapped around the JSON data.
See for example the code demonstrating this at https://gist.github.com/cowboy/1200708
The JSON data is supposed to remain as data. The service that you request the data from (a .php file, or something else) is what wraps the callback function around the data.
Here’s an example.
You make a JSON request to domain.com/users.php - the users.php file creates the JSON data, or retrieves it from somewhere else, and echo’s out the following:
{
"users": [
...
]
}
When you make a JSONP request to domain.com/users.php?callback=processData - the users.php file creates the JSON data, or retrieves it from somewhere else, just as above. It then uses the callback information from the URL, and wrapsthat name as a function call around the JSON data, so that the users.php file ends up outputting:
I should also mention, that because you are requesting the JSON data from the same domain that you are on, that you have no need to use JSONP. The reason for using that is as a security work-around, for when you are attempting to obtain JSON data from a domain that is different from yours.
which works fine… everything here is exactly the same except for the url to the JSON…
(one is regular JSON, other one JSONP wrapped in “callback” thing… )
That was an example, for the purpose of demonstration.
May I ask, why are you experimenting with JSONP when you currently have no reason to use it? I suggest that you stick with JSON and leave JSONP until you have the time and/or patience to learn more fully about it.
Ahh, okay. What happens with JSONP is that the result is placed inside of a script tag on your page, so that it ends up being:
<script>callback(...)</script>
Do you have a function called callback, that you have setup to handle that JSON data? If not, then that I presume is what the error message is referring to.
(but for what I need to do in my situation with JSONP right now, the JSON being provided to me remotely is wrapped in that “callback” thing, so I have to work with that…)
ok, is this confusing enough now? :~))
thank you very much for your help…
PS: ok, I see, I have to do the callback fn thing you say because the JSONP I’m pulling from is wrapped in that callback thing, right?
That is correct. Because you are attempting to retrieve the JSON content from another domain, the web browser does not allow that to happen as a normal JSON request. So instead, that data must be wrapped in a function call and plonked in to a <script> tag. The browser will then run that function, passing that JSON data to the function.
So if it was not callback(…) but instead doStuffWithData(…), your error message may-well instead say:
doStuffWithData is not defined…
For which you would need to have previously defined a function, such as:
function doStuffWithData(data) {
// do stuff with data in here
}
So I suggest that you do that with that callback function name that you have in your file.
@paul_wilkins - I was playing around with the code that maya90 shared last night and trying to solve that ‘callback in not defined’ error. In the end, I discovered that you can get around the fixed callback function with jQuery, but it seems a bit clunky:
all because of the JSONP I’m pulling from is wrapped in a “callback()” method…
oh man, this is grand…
oh well, I need to learn how to do this…
( any other ways you can think of doing this? that may be, better or whatever?)
so everything that I now have in getJSON() method has to go into a function that I call when the page loads?
doesn’t seem too different from calling getJSON() onload…
I’m still missing something here…