Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

json - JSON和JSONP有什么区别?(What are the differences between JSON and JSONP?)

格式明智,文件类型明智和实用明智吗?

  ask by Mohammad translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

JSONP is JSON with padding, that is, you put a string at the beginning and a pair of parenthesis around it.(JSONP是带填充的JSON,也就是说,你在开头放了一个字符串,在它周围放了一对括号。)

For example:(例如:)
//JSON
{"name":"stackoverflow","id":5}
//JSONP
func({"name":"stackoverflow","id":5});

The result is that you can load the JSON as a script file.(结果是您可以将JSON加载为脚本文件。)

If you previously set up a function called func , then that function will be called with one argument, which is the JSON data, when the script file is done loading.(如果您之前设置了一个名为func ,则在脚本文件加载完成后,将使用一个参数(即JSON数据)调用该函数。) This is usually used to allow for cross-site AJAX with JSON data.(这通常用于允许带有JSON数据的跨站点AJAX。) If you know that example.com is serving JSON files that look like the JSONP example given above, then you can use code like this to retrieve it, even if you are not on the example.com domain:(如果您知道example.com正在提供看起来像上面给出的JSONP示例的JSON文件,那么您可以使用这样的代码来检索它,即使您不在example.com域上:)
function func(json){
  alert(json.name);
}
var elm = document.createElement("script");
elm.setAttribute("type", "text/javascript");
elm.src = "http://example.com/jsonp";
document.body.appendChild(elm);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...