如何在coffeescript中使用HTTP GET从REST API获取JSON
How to do a HTTP GET in coffeescript to get a JSON from a REST API
我正在寻找一种简单的方法来获取Coffeescript中的REST API并获得json。我正在使用请求库。当我这样做的时候,什么也没有发生,没有错误。
request = require 'request'
resp = ""
request.get {uri:'https://api.service.co/search?query=paris', json : true}, (err, r, body) -> resp = body
console.log "BODY: " + resp
我做错了什么?你知道在coffeescript中从REST api获取json的更好方法吗?非常感谢!
我怀疑你的问题是request.get
异步执行,所以当它到达console.log
语句时,resp总是""
。
试试这个:
request = require 'request'
resp = ""
request.get {uri:'https://api.service.co/search?query=paris', json : true}, (err, r, body) ->
resp = body
console.log "BODY: " + resp
相关文章:
- Amazon S3 REST API大小不正确
- 客户端服务器REST API captcha实现
- 有没有一种方法可以防止img get请求使用css或js发生
- CORS-重定向到第二个GET正在接收的页面
- 通过REST API/GET请求订阅Mailchimp列表
- 通过REST API上的GET请求发送图像数据
- Kinvey URL REST GET 请求与相关表
- Rest api get call using jquery
- AngularJS:从http GET(来自REST服务)检索到的JSON数据中显示一个选择框
- YQL ODT-REST GET响应存在,但无法访问
- 将此REST GET请求集成到JQuery或JavaScript中
- AngularJS Rest GET请求不起作用
- 如何使用angular工厂从rest api返回http get请求
- HTTP GET请求JSON响应- REST API
- Angular JS REST API GET不显示数据
- 使用POST方法调用REST api会调用GET方法
- 如何在coffeescript中使用HTTP GET从REST API获取JSON
- 使用Angular服务的请求拦截器对REST get调用进行简单的身份验证
- 在REST服务中使用ContainerResponse筛选器时,GET正在工作,但POST不工作
- Angularjs的rest get方法在启用了.appcache的情况下无法工作