通过REST风格的JavaScript API的互动? [英] Interacting with RESTful API's via Javascript?

查看:120
本文介绍了通过REST风格的JavaScript API的互动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要开始了,我知道C ++,C#,Python和Ruby的部分,和基本的JavaScript。不管怎样,我的问题都是围绕着如何与通过Javascript API REST风格的交互。我一直没能找到在各网站的任何好的例子,所以我来到这里。

to start off, I know C++, C#, Python, some Ruby, and basic Javascript. Anyway, my question revolves around how to interact with RESTful API's via Javascript. I haven't been able to find any good examples on various websites, and so I've come here.

因此​​,我的基本问题是:我如何与REST的API的JS通过互动?和我在哪里可以找到如何在JS实现OAuth的?我知道如何让我的钥匙和这样的,只是没怎么竟在code它们。

So my basic question is: How do I interact with RESTful API's via JS? And where can I find out how to implement OAuth in JS? I know how to get my keys and such, just not how to actually code them in.

下面是我的MAC终端卷曲运行一个Twitter的API状态更新的一个例子:

Below is an example of a twitter API status update run from my MAC terminal with curl:

curl -u username:password 
-d "my tweet" 
http://api.twitter.com/1/statuses/update.json

如何在Javascript(preferably与OAuth认证)实现这个?这至少是重新开始我的正确的方向前进。

How can I implement this in Javascript (preferably with OAuth authentication)? This would at least start me going in the right direction.

非常感谢!!

Thanks so much!!

推荐答案

的问题是,你需要使用AJAX来查询远程REST API和AJAX只允许查询在同一个域中的页面资源。所以, api.twitter.com 的请求会失败,因为它是比你的服务器不同的域。

The problem is that you will need to use AJAX to query the remote REST API, and AJAX is only allowed to query resources on the same domain as the page. So, a request to api.twitter.com will fail because it is on a different domain than your server.

要纠正这一点,你将需要code服务器,以便叽叽喳喳的请求。但是,您可以创建自己的AJAX存根,这将直接从您的网页接收数据,然后生成/发送使用由您的客户提供数据的请求,Twitter的服务器端。

To correct this you will need to code your server to make the request to twitter. You can however create your own AJAX stubs that will accept data directly from your page, and then build / send requests to twitter server-side using data supplied by your client.

这篇关于通过REST风格的JavaScript API的互动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆