关于JSON-P格式的3个问题

发布于 2024-08-29 11:34:04 字数 1206 浏览 2 评论 0原文

我必须编写一个将托管在不同域上的脚本。该脚本必须从我的服务器获取信息。

因此,stackoverflow 的用户告诉我,我必须使用 JSON-P 格式,经过研究,这就是我要做的。 (JSON-P 中提供的数据用于在其他网站上显示我的服务器上托管的一些信息)

  1. 如何从我的服务器输出 JSON-P?它与 PHP

    中的 json_encode 函数相同吗?
  2. 如何设计输出 JSON-P 的树模式 (你知道,比如:({“name”:“foo”,“id”:“xxxxx”,“blog”:“http ://xxxxxx.com"}); 我可以从我的 XML 输出中窃取这个吗? (http://bit.ly/9kzBDP)

  3. 每次访问者浏览我的网站时widget 是它会在我的服务器上发出请求,请求在客户端显示 JSON-P 数据。 这会显着增加 CPU 负载(网站上的 1 位访问者将在我的服务器上执行脚本 = 1 个 SQL 请求来输出数据),因此有什么方法可以“缓存” JSON-P 信息输出每天仅刷新一到两次并将其存储到“文件”中(在哪个扩展名中?)。

但另一方面我想说直接请求 JSON-P 数据(不缓存它)是一个优点,因为集成脚本的网站只想显示他们的信息< /strong> 而不是全部数据。因此,制作一个类似以下内容的脚本:

jQuery.getJSON("http://www.something.com/json-p/outpout?filter=5&callback=?", function(data) {
................);
});

其中过滤器=网站想要显示的信息。

  • 额外问题:我必须使用 JS 框架来读取 JSON-P 吗?或者没有任何框架包含在页面上的纯 JS 可以做到这一点吗?

你怎么认为 ?

非常感谢 ;)

I have to write a script which will be hosted on differents domains. This script has to get information from my server.

So, stackoverflow's user told me that i have to use JSON-P format, which, after research, is what i'm going to do. (the data provided in JSON-P is for displaying some information hosted on my server on other website)

  1. How do I output JSON-P from my server ? Is it the same as the json_encode function from PHP

  2. How do i design the tree pattern for the output JSON-P
    (you know, like : ({"name" : "foo", "id" : "xxxxx", "blog" : "http://xxxxxx.com"});
    can I steal this from my XML output ? (http://bit.ly/9kzBDP)

  3. Each time a visitor browse a website on which my widget is it'll make a request on my server, requesting the JSON-P data to display on the client side.
    It'll increase dramatically the CPU load (1 visitor on the website who will have the script = 1 SQL request on my server to output data), so is there any way to 'caching' the JSON-P information output to refresh it only one or twice a day and stores it into a 'file' (in which extension?).

BUT on the other hand i would say that requesting the JSON-P data directly (without caching it) is a plus, because, websites which will integrates the script only want to display THEIR information and not the whole data. So, making a script with something like that:

jQuery.getJSON("http://www.something.com/json-p/outpout?filter=5&callback=?", function(data) {
................);
});

Where filter= the information the website wants to display.

  • Bonus question : do I have to use a JS framework to read JSON-P ? or JS pure without any framework to include on the page can do that ?

What do you think ?

Thank you very much
;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迟月 2024-09-05 11:34:04

JSONP 请求

$.ajax({
  type: "get",
  dataType: "jsonp",
  url: "mothership.com/widget_data",
  data: {whatever: "here", requested_from: "someonesblog.com" },
  cache: true,
  success: function(data, textStatus, XMLHttpRequest){
    // respond to ajax call here

    // debug: make sure your data is getting loaded properly
    console.log(data);

    // do other necessary things here
  }
});

从服务器编码并输出 JSON

<?php
  header("Content-Type: application/json");

  # You can access data from the jQuery.ajax() call with $_GET here

  $data = array(
    "name" => "foo",
    "id"   => 1234,
    "blog" => $_GET["requested_from"]
  );
  echo $_GET["callback"] . "(" . json_encode($data). ");" ;

  # this will output something like
  # jsonp1255285823({"name":"foo","id":1234,"blog":"someonesblog.com"});

  exit;
?>

JSONP request

$.ajax({
  type: "get",
  dataType: "jsonp",
  url: "mothership.com/widget_data",
  data: {whatever: "here", requested_from: "someonesblog.com" },
  cache: true,
  success: function(data, textStatus, XMLHttpRequest){
    // respond to ajax call here

    // debug: make sure your data is getting loaded properly
    console.log(data);

    // do other necessary things here
  }
});

Encode and Output JSON from server

<?php
  header("Content-Type: application/json");

  # You can access data from the jQuery.ajax() call with $_GET here

  $data = array(
    "name" => "foo",
    "id"   => 1234,
    "blog" => $_GET["requested_from"]
  );
  echo $_GET["callback"] . "(" . json_encode($data). ");" ;

  # this will output something like
  # jsonp1255285823({"name":"foo","id":1234,"blog":"someonesblog.com"});

  exit;
?>
黑凤梨 2024-09-05 11:34:04

您可以使用 json_encode 函数获取对象的 JSON 字符串表示形式,然后在其周围添加填充,即:

$json = json_encode($myObj);
echo $callback . "(" . $json . ");";

大多数 JSON 结构紧密复制 XML 结构和命名约定。没有后代的单个节点成为属性,重复的 XML 节点将是 JSON 数组,有后代的节点是对象。请参阅 http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid =396 以获得以两种格式表示的数据的体面的并排视图。

至于在服务器上缓存数据,如果您的流量不太大,大多数缓存方法可能有点矫枉过正。请参阅此答案之一我自己的问题和这个推荐memcached的答案 如果您仍然坚持需要它。您可以使用客户端缓存控制标头expires来确保客户端即使跨多个页面/刷新也只获取一次数据。

额外的答案:JSON-P 的美妙之处在于您不需要任何库来解析它。格式是纯 javascript,将 JSON-P 添加到页面就像将脚本添加到页面一样简单:

<script 
   type="text/javascript" 
   src="http://myurl.com/jsonp.php?callback=test&filter=5">
</script>

You can use the json_encode function to get a JSON string representation of an object and then add the padding around it, ie:

$json = json_encode($myObj);
echo $callback . "(" . $json . ");";

Most JSON structures closely copy XML structures and naming conventions. Single nodes with no descendants become properties, repeated XML nodes would be JSON arrays and nodes with descendants are objects. See http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=396 for a decent, side-by-side view of data represented in both formats.

As for caching the data at the server, most caching methods can be a little overkill if your traffic isn't too heavy. See this answer to one of my own questions and this answer recommending memcached if you're still insistent that you need it. You can use the client cache control header expires to make sure the client fetches the data only once even across multiple pages/refreshes.

Bonus answer: the beauty of JSON-P is that you need no library whatsoever to parse it. The format is pure javascript and adding JSON-P to a page is as simple as adding a script to the page:

<script 
   type="text/javascript" 
   src="http://myurl.com/jsonp.php?callback=test&filter=5">
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文