Javascript
For example, I have the following JavaScript code (Dojo 1.6 is already loaded):
dojo.require("dojo.io.script") // PART I var jsonpArgs = { url: "http://myapp.appspot.com/query", content: { id: "1234", name: "Juan", start_date: "2000-01-01", callback: "recover" } }; // PART II dojo.io.script.get(jsonpArgs).then(function(data) { console.log(data); }); // PART III function recover(data) { console.log(data); }
Direct request from browser
I understand that my server will receive a request as if I had entered the following into the address bar:
http:
Expected response
If I directly requested my server using the address bar of the browser, I will get something like this in the application/json MIME type and in the text editor in the browser:
recover( { id: 1234, name: Juan, data: [ ["2000-01-01", 1234], ["2000-01-02", 5678] ] } );
Problem
Now, looking back at JavaScript Part II, I would execute a JSONP request using dojo.io.script.get(jsonpArgs) . This returns a Deferred object that I can use by clinging .then after it. Note that I defined a .then event handler for the output that captured data on the console.
However, all I get in the console is Event . I tried to find its data tree, but I could not find the data that I was expecting.
Question
- Where is the response to the JSONP request stored? How to find him?
- My server (which I control) displays only a textual visualization of the requested data wrapped in a
callback function (here indicated as recover ) and indicates the type of application/json MIME. Is there anything else I need to configure on my server so that the response data is captured by the Deferred object?
Attempt to solve
I really can restore the response by defining a callback function (in this case, recover in JavaScript part III). However, in Dojo's tutorials, they simply recovered data using the Deferred (and .then ) structure. How to do this using Dojo Deferred s?
Update (using the Twitter example from the Dojo tutorial)
Take for example this script from the Dojo tutorial Getting Jiggy using JSONP . I edited it to write data to the console.
dojo.require("dojo.io.script"); dojo.io.script.get({ url: "http://search.twitter.com/search.json", callbackParamName: "callback", content: {q: "#dojo"} }).then(function(data){
For console.log(data) I get an Object , not an Event , as shown in my case. Since the example means that the data is in data.results , I am also trying to view this tree, but I do not see the expected data from Twitter. I'm at a loss.
For console.log(data.results) , I get an array of Object s. If I directly access Twitter, this is what I would get in the clear. Each Object contains regular tweet metadata, such as username, time, custom portrait, and tweet itself. Easy.
This one hits me right on the head. The .then chain handler, an anonymous function, receives only one data argument. But why is the results property in console.log(data) and the return object that I get from console.log(data.results) different ?