Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JSONP我做得对吗?_Javascript_Jsonp - Fatal编程技术网

Javascript JSONP我做得对吗?

Javascript JSONP我做得对吗?,javascript,jsonp,Javascript,Jsonp,我的主要剧本里有这个 (function(d){ var shortURLjs, id = 'shortUrl'; if (d.getElementById(id)) {return;} shortURLjs = d.createElement('script'); shortURLjs.id = id; shortURLjs.async = true; shortURLjs.src = "http://site.com/test.js";

我的主要剧本里有这个

(function(d){
    var shortURLjs, id = 'shortUrl'; 
    if (d.getElementById(id)) {return;}
    shortURLjs = d.createElement('script'); 
    shortURLjs.id = id; 
    shortURLjs.async = true;
    shortURLjs.src = "http://site.com/test.js";
    d.getElementsByTagName('body')[0].appendChild(shortURLjs);
}(document));
在test.js中我有

shortURL = { "firstName": "John", "lastName" : "Smith"};
但是,当我在添加test.js文件后尝试从包含该文件的脚本中访问shortURL时,它没有定义


我有点困惑。

您的主函数必须调用这样的函数,它将作为脚本标签添加到您的头部:

<script src="http://www.mydomain.com/file.php?callback=JSONP_callback"></script>
然后,当您使用php或任何脚本时,您必须从回调中调用该函数,只要Javascript能够识别该函数,它可以是任何函数:

// Within PHP it looks like:
echo $_GET['callback'] . "(" . json_encode(array( "name" => "Niels")) . ")";

// Which results in:
JSONP_callback({ name : "Niels" });
它的结果是函数名,因为我们调用了带有
callback=JSONP\u callback
作为参数的页面。由于我们在主页中定义了函数JSONP_callback(result),脚本将使用给定的数据执行此函数


几个月前,我不得不为学校研究这个,我制作的演示,也许你可以用它:

你的主要功能必须调用这样的东西,它将作为脚本标签添加到你的头部:

<script src="http://www.mydomain.com/file.php?callback=JSONP_callback"></script>
然后,当您使用php或任何脚本时,您必须从回调中调用该函数,只要Javascript能够识别该函数,它可以是任何函数:

// Within PHP it looks like:
echo $_GET['callback'] . "(" . json_encode(array( "name" => "Niels")) . ")";

// Which results in:
JSONP_callback({ name : "Niels" });
它的结果是函数名,因为我们调用了带有
callback=JSONP\u callback
作为参数的页面。由于我们在主页中定义了函数JSONP_callback(result),脚本将使用给定的数据执行此函数


几个月前,我不得不为学校研究这个,我制作的演示,也许你可以用它:

问题是脚本标记异步加载,而不是像这样运行

insert tag
set shortURL
use shortURL
//your script finishes
插入的脚本只有在完成后才有机会运行

insert tag
use shortURL //oops!
//your script finishes
set shortURL
JSONP背后的诀窍是,我们不返回普通数据,而是返回看起来像函数调用的数据

//instead of setting a variable like "shortUrl = ..."
// we instead call a function:
on_have_shortUrl({"first_name":...});
因此,您所需要做的就是预先准备好一个
on\u have\u shortUrl
函数

function on_have_shortUrl(shortURL){ use shortURL here}
insert script tag
inserted script calls your function passing the data to it

Niel的回答更详细地介绍了JSONP“协议”如何让您为回调函数和其他类似的东西选择不同的名称。

问题是脚本标记异步加载,而不是像这样运行

insert tag
set shortURL
use shortURL
//your script finishes
插入的脚本只有在完成后才有机会运行

insert tag
use shortURL //oops!
//your script finishes
set shortURL
JSONP背后的诀窍是,我们不返回普通数据,而是返回看起来像函数调用的数据

//instead of setting a variable like "shortUrl = ..."
// we instead call a function:
on_have_shortUrl({"first_name":...});
因此,您所需要做的就是预先准备好一个
on\u have\u shortUrl
函数

function on_have_shortUrl(shortURL){ use shortURL here}
insert script tag
inserted script calls your function passing the data to it

Niel的回答更详细地介绍了JSONP“协议”如何让您为回调函数和其他类似的东西选择不同的名称。

我想我理解。但是,如果页面上包含脚本,那么该脚本中的变量是否不可访问?或者这是否会跨越到域策略中?您必须仅将head标记视为函数的调用方,在该函数中,您可以执行任何您想要的操作,因为它位于主页面上。脚本标签只提供主页的数据。我想我明白了。但是,如果页面上包含脚本,那么该脚本中的变量是否不可访问?或者这是否会跨越到域策略中?您必须仅将head标记视为函数的调用方,在该函数中,您可以执行任何您想要的操作,因为它位于主页面上。脚本标记只提供主页的数据。@ŠimeVidas:neh,reddit会使用“钉住它?”:)@ŠimeVidas:neh,reddit会使用“钉住它?”:)