Where is the response stored after the Dojo JSONP request?

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://myapp.appspot.com/query?id=1234&name=Juan&start_date=2000-01-01&callback=recover 

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){ //we're only interested in data.results, so strip it off and return it console.log(data); // I get an Object, not an Event, but no Twitter data when browsing the results property console.log(data.results) // I get an array of Objects return data.results; }); 

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 ?

+4
source share
1 answer

I understood.

Manual callback implementation

 function recover(data) { console.log(data); } var jsonpArgs = { url: "http://myapp.appspot.com/query", content: { id: "1234", name: "Juan", start_date: "2000-01-01", callback: "recover" }; dojo.io.script.get(jsonpArgs); 

This is the request my server will receive:

 http://myapp.appspot.com/query?id=1234&name=Juan&start_date=2000-01-01&callback=recover 

In this case, I expect the following output from my server:

 recover({ id: 1234, name: Juan, data: [ ["2000-01-01", 1234], ["2000-01-02", 5678] ] }); 

Three notes:

  • The server will expect a callback in the request URL string. callback is implemented as a jsonpArgs property.
  • Since I specified callback=recover , my server will add recover( + the_data_I_need + ) , will return the entire string to the browser, and the browser will perform recover(the_data_I_need) . It means...
  • To determine, for example, function recover(one_argument_only) {doAnythingYouWantWith(one_argument_only)}

The problem with this approach is that I cannot use the Deferred chain with .then . For instance:

 dojo.io.script.get(jsonpArgs).then(function(response_from_server) { console.log(response_from_server); }) 

This will give me an Event , with no trace of the expected response at all.


Using Dojo JSONP Request Implementation

 var jsonpArgs = { url: "http://myapp.appspot.com/query", callbackParamName: "callback", content: { id: "1234", name: "Juan", start_date: "2000-01-01" }; dojo.io.script.get(jsonpArgs); 

This is the request my server will receive:

 http://myapp.appspot.com/query?id=1234&name=Juan&start_date=2000-01-01&callback=some_function_name_generated_by_dojo 

In this case, I expect the following output from my server:

 some_function_name_generated_by_dojo({ id: 1234, name: Juan, data: [ ["2000-01-01", 1234], ["2000-01-02", 5678] ] }); 

Notes:

  • Note the jsonpArgs property, callbackParamName . The value of this property should be the name of the variable (in the request URL string) expected by the server. If my server expects a callbackfoo , then callbackParamName: "callbackfoo" . In my case, my server expects the name callback , so callbackParamName: "callback" .

  • In the previous example, I specified the callback=recover request URL and went on to implement function recover(...) {...} . This time I do not need to worry about it. Dojo inserts its own function callback=some_function_name_generated_by_dojo .

  • I believe some_function_name_generated_by_dojo is defined as:

Definition:

 function some_function_name_generated_by_dojo(response_from_server) { return response_from_server; } 

Of course, the definition is not so simple, but the advantage of this approach is that I can use the Dojo Deferred structure. See the code below, which is identical to the previous example:

 dojo.io.script.get(jsonpArgs).then(function(response_from_server) { console.log(response_from_server); }) 

This will give me the exact data I need:

 { id: 1234, name: Juan, data: [ ["2000-01-01", 1234], ["2000-01-02", 5678] ] } 
+6
source

Source: https://habr.com/ru/post/1390787/


All Articles