通过 Javascript 与 RESTful API 交互? [英] Interacting with RESTful API's via Javascript?

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

问题描述

首先,我了解 C++、C#、Python、一些 Ruby 和基本的 Javascript.无论如何,我的问题围绕着如何通过 Javascript 与 RESTful API 进行交互.我一直无法在各种网站上找到任何好的例子,所以我来到了这里.

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.

所以我的基本问题是:如何通过 JS 与 RESTful API 交互?我在哪里可以找到如何在 JS 中实现 OAuth?我知道如何获得我的钥匙之类的东西,只是不知道如何实际编码它们.

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.

以下是使用 curl 从我的 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 中实现这一点(最好使用 OAuth 身份验证)?这至少会让我朝着正确的方向前进.

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

非常感谢!!

推荐答案

问题是你将需要使用 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.

要更正此问题,您需要对服务器进行编码以向 Twitter 发出请求.但是,您可以创建自己的 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.

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

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