jsonp 使用

    技术2022-07-11  130

    jsonp 使用

    I've been annoyed that many popular APIs have moved to require authentication in order to retrieve information.  If I can browse a page and get said information, why can't I simply pull it with some simple code and skip the authentication bit?  I had thought that both Twitter and Facebook required authentication to retrieve basic post counts, but it turns out you can grab those with JSONP.  Here's how you do it.

    我很恼火,许多流行的API都已移动到要求身份验证才能检索信息的位置。 如果我可以浏览页面并获得上述信息,为什么不能简单地用一些简单的代码将其拉出并跳过身份验证位? 我以为Twitter和Facebook都需要身份验证才能检索基本帖子数,但事实证明,您可以使用JSONP来获取这些内容。 这是您的操作方式。

    JavaScript (The JavaScript)

    I'm going to use basic JavaScript to show you how to do this:

    我将使用基本JavaScript向您展示如何执行此操作:

    // Tiny object for getting counts var socialGetter = (function() { /* just a utility to do the script injection */ function injectScript(url) { var script = document.createElement('script'); script.async = true; script.src = url; document.body.appendChild(script); } return { getFacebookCount: function(url, callbackName) { injectScript('https://graph.facebook.com/?id=' + url + '&callback=' + callbackName); }, getTwitterCount: function(url, callbackName) { injectScript('http://urls.api.twitter.com/1/urls/count.json?url=' + url + '&callback=' + callbackName); } }; })(); // Callbacks to do something with the result function twitterCallback(result) { result.count && console.log('The count is: ', result.count); } function facebookCallback(result) { result.shares && console.log('The count is: ', result.shares); } // Usage socialGetter.getFacebookCount('https://davidwalsh.name/twitter-facebook-jsonp', 'facebookCallback'); socialGetter.getTwitterCount('https://davidwalsh.name/twitter-facebook-jsonp', 'twitterCallback');

    There are hundreds of all-encompassing to tiny micro-frameworks that manage JSONP so the most important part of this post is likely the URLs to  retrieve counts from. Use whatever tool you think is best!

    有数百种管理微型JSONP的微型框架,因此这篇文章中最重要的部分可能是从中检索计数的URL。 使用您认为最好的任何工具!

    Twitter and Facebook surely limit the rate at which you can make these requests, so it's very possible that JSONP is just the wrong route to go if you run a popular site.  One quick solution is to store the count values in sessionStorage, but that's only a per-user basis.  If you run a site that gets decent traffic, your best bet if probably grabbing the count on the server side and caching it for a given period of time.

    Twitter和Facebook肯定会限制您发出这些请求的速度,因此,如果您运行一个受欢迎的网站,JSONP很可能是错误的选择。 一种快速的解决方案是将计数值存储在sessionStorage中,但这仅基于每个用户。 如果您运行的网站流量不错,那么最好的选择是抓住服务器端的计数并将其缓存给定的时间。

    翻译自: https://davidwalsh.name/twitter-facebook-jsonp

    jsonp 使用

    相关资源:jdk-8u281-windows-x64.exe
    Processed: 0.010, SQL: 9