在Dojo JSONP请求之后,响应是否存储在哪里? [英] Where does the response get stored after a Dojo JSONP request?
问题描述
例如,我有以下JavaScript代码(Dojo 1.6已经加载):
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 query from browser
I understand that my server will receive the query as though I typed the following into the address bar:
http://myapp.appspot.com/query?id=1234&name=Juan&start_date=2000-01-01&callback=recover
预期响应
如果我直接使用浏览器地址栏查询我的服务器,我将收到MIME类型应用程序/ json
和浏览器中呈现的明文,如下所示:
Expected response
If I directly queried my server using the browser address bar, I'll receive, in MIME type application/json
and plaintext rendered in browser, something like this:
recover(
{
id: 1234,
name: Juan,
data: [
["2000-01-01", 1234],
["2000-01-02", 5678]
]
}
);
问题
现在回头看JavaScript的第二部分,我将使用 dojo.io.script.get(jsonpArgs)
执行JSONP请求。这将返回一个 Deferred
对象,我可以通过链接 .then
来获取优势。请注意,我定义了 .then
事件的处理程序,以将捕获的数据
输出到控制台。
Problem
Now, looking back at Part II of the JavaScript, I'd execute the JSONP request with dojo.io.script.get(jsonpArgs)
. This returns a Deferred
object, which I can take advantage of by chaining .then
after it. Note that I defined the handler for the .then
event to output that captured data
to the console.
但是,我所有的控制台都是一个事件
。我试图搜索其数据树,但是我找不到我预期的数据。
However, all I get in the console is an Event
. I tried to search its data tree, but I could not find the data I expected.
- 存储JSONP请求的响应在哪里?我如何找到?
- 我的服务器(我控制的)只输出要求的数据的明文渲染,包裹在
回调
函数(这里指定为recover
),并指定一个application / json
MIME类型。还有什么我需要设置在我的服务器上,以便响应数据由Deferred
对象捕获?
- Where is the response for a JSONP request stored? How do I find it?
- My server (which I control) only outputs a plaintext rendering of the data requested, wrapped in the
callback
function (here specified asrecover
), and specifies aapplication/json
MIME type. Is there anything else I need to set up on my server, so that the response data is captured by theDeferred
object?
尝试解决方案
我可以通过定义回调函数实际恢复响应(在这种情况下 recover
在JavaScript的第三部分)。但是,在Dojo教程中,他们只是使用 Deferred
(和 .then
)框架恢复数据。如何使用Dojo Deferred
s?
Attempted solution
I can actually recover the response by defining the callback function (in this case recover
in Part III of the JavaScript). However, in the Dojo tutorials, they just recovered the data using the Deferred
(and .then
) framework. How do I do it using Dojo Deferred
s?
以Dojo教程为例,这个脚本。我编辑它将数据记录到控制台。
Take for example this script from the Dojo tutorial, Getting Jiggy With JSONP. I edited it to log 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;
});
对于 console.log(data)
我得到一个对象
,而不是一个事件
。由于该示例意味着数据位于 data.results
中,我还尝试浏览此树,但是我没有看到我的预期数据。我很失落。
For console.log(data)
, I get an Object
, not an Event
as illustrated by my case. Since the example implies that the data resides in data.results
, I also try to browse this tree, but I don't see my expected data from Twitter. I'm at a loss.
对于 console.log(data.results)
,我得到一个对象
秒。如果我直接查询Twitter,这是我明白的。每个 Object
包含通常的tweet元数据,如用户名,时间,用户肖像和tweet本身。很容易。
For console.log(data.results)
, I get an array of Object
s. If I query Twitter directly, this is what I'd get in plaintext. Each Object
contains the usual tweet meta-data like username, time, user portrait, and the tweet itself. Easy enough.
这个人在头上碰到我。 .then
链接的一个匿名函数的处理程序只接收一个参数 data
。但是为什么在
返回的对象,我从 console.log(data)
和中的结果 console.log(data.results)
中得到的不同?
This one hits me right on the head. The handler for the .then
chain, an anonymous function, receives only one argument data
. But why is it that the results
property in console.log(data)
and the returned object I get from console.log(data.results)
are different?
推荐答案
我得到了。
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 that my server will receive:
http://myapp.appspot.com/query?id=1234&name=Juan&start_date=2000-01-01&callback=recover
在这种情况下,我会期望从我的服务器输出以下内容:
In this case, I'll expect the following output from my server:
recover({
id: 1234,
name: Juan,
data: [
["2000-01-01", 1234],
["2000-01-02", 5678]
]
});
需要注意的三件事:
- 服务器将在查询URL字符串中期望
回调
。回调
被实现为jsonpArgs
的属性。 - 因为我指定了
callback = recover
,我的服务器将附加recover(
+the_data_I_need
+)
,将整个字符串返回到浏览器,浏览器将执行recover(the_data_I_need)
。这意味着... - 我必须定义,例如
function recover(one_argument_only){doAnythingYouWantWith(one_argument_only)}
- Server will expect
callback
in the query URL string.callback
is implemented as a property ofjsonpArgs
. - Because I specified
callback=recover
, my server will attachrecover(
+the_data_I_need
+)
, returns the whole string to the browser, and browser will executerecover(the_data_I_need)
. This means... - That I'll have to define, for example,
function recover(one_argument_only) {doAnythingYouWantWith(one_argument_only)}
这种方法的问题是我无法利用 Deferred
链接使用 .then
。例如:
The problem with this approach is that I cannot take advantage of Deferred
chaining using .then
. For example:
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.
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 that 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'll 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]
]
});
需要注意的事项:
-
请注意
jsonpArgs
,callbackParamName
的属性。该属性的值必须是服务器预期的变量(在查询URL字符串中)的名称。如果我的服务器期望callbackfoo
,那么callbackParamName:callbackfoo
。在我的情况下,我的服务器期望名称回调
,因此callbackParamName:callback
。
Note the property of
jsonpArgs
,callbackParamName
. The value of this property must be the name of the variable (in the query URL string) expected by the server. If my server expectscallbackfoo
, thencallbackParamName: "callbackfoo"
. In my case, my server expects the namecallback
, thereforecallbackParamName: "callback"
.
在上一个例子中,我在查询网址 callback = recover
中指定并继续实现函数恢复(...){...}
。这一次,我不需要担心。 Dojo将插入自己的首选功能 callback = some_function_name_generated_by_dojo
。
In the previous example, I specified in the query URL callback=recover
and proceeded to implement function recover(...) {...}
. This time, I do not need to worry about it. Dojo will insert its own preferred function callback=some_function_name_generated_by_dojo
.
我想象 some_function_name_generated_by_dojo
定义为:
定义:
function some_function_name_generated_by_dojo(response_from_server) {
return response_from_server;
}
当然定义不是那么简单,但是这种方法的优点是我可以利用Dojo的延期框架。请参阅下面的代码,与前面的示例相同:
Of course the definition is not that simple, but the advantage of this approach is that I can take advantage of Dojo's Deferred framework. 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]
]
}
这篇关于在Dojo JSONP请求之后,响应是否存储在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!