Javascript Widgets 获取外部 JSON/XML ...如何?
我想为网页构建一个小型嵌入式小部件,该小部件将访问提供 XML 或 JSON 服务的 API(我可以在这两种格式之间进行选择)。我想做的是让用户只需选择一段代码并将其粘贴到网站中,就像 Twitter 小部件
由于跨域安全问题,我不能只进行 ajax 调用来加载这些外部数据集,所以我只能使用 iFrame 吗?我更喜欢输出更灵活的东西,以便用户可以通过 CSS 更改它。
除了在宿主站点上开放跨域权限之外,还有什么可以做的吗?
I would like to build a little embeddable widget for webpages that will access an API the serves up XML or JSON (I can choose between those two formats). What I'd like to do is have the user just select a block of code and paste it into the website, much like the Twitter Widgets
Due to cross-domain security issues I can't just do an ajax call to load those external data sets, so am I down to just using an iFrame? I'd prefer something with a bit more flexibility in the output so that users could change it via CSS.
Is there anything that can be done other than opening the cross-domain privileges on the host site?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
了解jsonp。
一般来说,一个过于简单的实现,只是为了解释它是如何工作的,将是:
在客户端上:
在服务器上,例如在 aspx
或 Php 中
限制:
1 - 没有帖子。只获取。发送到服务器的数据仅限于 URL
2 中适合的内容 - 服务器的响应不是纯 JSON - 它必须包装在回调函数中,客户端为服务器提供其名称 - 在本例中 - 使用r 查询字符串参数。
玩得开心
Read about jsonp.
Generally, an oversimplified implementation, just to explain how it works would be:
on the client:
on the server, for example in aspx
or in Php
The limitations:
1 - no post. only GET. data sent to the server is limited to what fits in a URL
2 - the response of the server is not pure JSON - it has to be wrapped in a callback function, that the client gives the server it's name - in this example - using the r query-string parameter.
Have Fun
JSONP 是你的朋友。只需在动态添加的链接脚本中发回数据即可。
首先,数据请求由客户端通过以下方式发送到您的服务器:
然后,请求您计算机上的服务器端数据。以 JavaScript 形式返回响应。类似于:
该脚本被加载到之前创建的脚本元素中。
DataResponse()
在请求客户端上调用。JSONP is your friend. Just send back the data in a dynamically added linked script.
First, Data request is sent by the client to your server via something like:
Then, request the data server-side on your machine. Return the response as JavaScript. Something like:
That script is loaded into the script element created earlier.
DataResponse()
is called on the requesting client.