CrunchBase API 和 jQuery $.getJSON 的问题
我试图简单地发送带有“名称”的警报,但它似乎不起作用。建议?
$(document).ready(function() {
$.getJSON("http://api.crunchbase.com/v/1/companies/permalink?name=Google", function(data) {
alert("Hello: " + data.name);
});
});
以下是 JSON 包含的内容:
{"crunchbase_url": "http://www.crunchbase.com/company/google",
"permalink": "google",
"name": "Google"}
I am trying to simply send an alert with a "name", but It doesn't seem to work. Advice?
$(document).ready(function() {
$.getJSON("http://api.crunchbase.com/v/1/companies/permalink?name=Google", function(data) {
alert("Hello: " + data.name);
});
});
Here is what the JSON contains:
{"crunchbase_url": "http://www.crunchbase.com/company/google",
"permalink": "google",
"name": "Google"}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不是 Crunchbase,则无法发送该请求。出于安全原因,只有
www.crunchbase.com
可以向www.crunchbase.com
发送 AJAX 请求。 (想象一下我们正在谈论www.bankofamerica.com
并且我已登录。如果任何网站都可以向www.bankofamerica.com
发送任意请求,那将是一个问题> 附有我的 Cookie。)API 文档 确实但是,指示 JSON-P 选项。如果你有兴趣,你可以查看实现细节,但重要的是 JSON-P 技术能够绕过域名限制,因为 Crunchbase 的 API 允许你这样做。
URL
http://api.crunchbase.com/v/1/company/google.js
是与您提到的类似的资源,但这个特定的 URL 允许您使用 JSON-P,而你发布的那个则没有。 jQuery 允许您轻松完成此操作:将 URL 作为http://api.crunchbase.com/v/1/company/google.js?callback=?< 传递给
$.getJSON
/code> (有关回调的部分很重要!),jQuery 将填补空白并在幕后处理加载的神奇脚本。非常奇特:)If you're not Crunchbase, you can't send that request. For security reasons, only
www.crunchbase.com
may send AJAX requests towww.crunchbase.com
. (Imagine we were talking aboutwww.bankofamerica.com
and I were logged in. It'd be a problem if just any site could send arbitrary requests towww.bankofamerica.com
with my cookies attached.)The API documentation does indicate a JSON-P option, however. If you're interested, you can look up the implementation details, but the important bit is that the JSON-P technique is able to circumvent the domain name restriction because Crunchbase's API will allow you to.
The URL
http://api.crunchbase.com/v/1/company/google.js
is a similar resource to the one you mentioned, but this particular URL allows you to use JSON-P, whereas the one you posted does not. jQuery allows you to do this easily: pass the URL to$.getJSON
ashttp://api.crunchbase.com/v/1/company/google.js?callback=?
(the bit about the callback is important!), and jQuery will fill in the blanks and handle the magic script loading behind the scenes. It's very fancy :)通常您可以通过在 URL 中添加
&callback=?
来使用 JSONP,但是在这种情况下不支持 JSONP 回调,来自 crunchbase API 文档:底线是最重要的,您将看到这个: http://api.crunchbase.com /v/1/companies/permalink?name=Google&callback=callme 仍然会生成常规 JSON,而不是 JSONP。
Normally you'd use JSONP here by adding
&callback=?
to your URL, however a JSONP callback is not supported in this case, from the crunchbase API documentation:The bottom line is the most important, you'll see that this: http://api.crunchbase.com/v/1/companies/permalink?name=Google&callback=callme still results in regular JSON, not JSONP.