简体   繁体   English

如何发送发布请求并在Rails中接收json

[英]how to send a post request and receive json back in rails

I've looked through a fair amount of the questions - yet, I am still confused. 我已经仔细研究了很多问题-但是,我仍然感到困惑。

I am making a Rails app that connects with the Healthgraph API (here http://developer.runkeeper.com/healthgraph/registration-authorization ). 我正在制作一个与Healthgraph API(此处为http://developer.runkeeper.com/healthgraph/registration-authorization )连接的Rails应用程序。

I am supposed to make a POST request via Rails - based on a parameter I recieve and then recieve the JSON. 我应该通过Rails发出POST请求-基于我接收的参数,然后接收JSON。 I have tried the outh2, runkeeper and other gems and they don't work because of a certain dependency. 我已经尝试过outh2,runkeeper和其他gems,但由于某种依赖性它们无法工作。 However, I am lost on how to POST via Rails and recieve a response. 但是,我不知道如何通过Rails进行POST并收到响应。

I do get the code back in my controller - however, I still don't know how to get an access token. 我确实在控制器中获取了代码-但是,我仍然不知道如何获取访问令牌。

def rktest 防御测试

respond_to

if params[:code]
  require 'oauth2'

  @code=params[:code]

  @cc_id=client_id
  @client_s=client_secret

else

  @code="None"

end

I'm trying to also do it via Javascript - but I'd rather not - since it leaves my client secret exposed. 我也尝试通过Javascript来执行此操作-但我不希望这样做-因为它会使我的客户秘密暴露无遗。 Also, I don't recieve data back either. 另外,我也不会收到数据。

<script type="text/javascript" src="javascripts/jquery-1.7.1.min.js"></script>

<script type="text/javascript">

function get_access_token(code,ccid,cc_s){

$.ajaxSetup({
'beforeSend': function(xhr){
xhr.setRequestHeader("Accept", "text/javascript")
}
});

var sent = { 'grant_type': 'authorization_code',
'code': code,
'client_id': ccid,
'client_secret': cc_s,
'redirect_uri': 'http://myurl.com/rktest'
};

document.write("<p>" + sent + "</p>");

$.ajax({
url: 'https://runkeeper.com/apps/token',
data: sent,
type: 'POST',
dataType: 'json',
success: function(data, status, xhr){
if (status === 'error' || !xhr.responseText){
handleError();
}else{

document.write("<p>" + data + "</p>");
document.write("<p>" + status + "</p>");
document.write("<p>" + xhr + "</p>");

}
}
});

}

<%= 'get_access_token(\''+@code+'\',\''+@cc_id+'\',\''+@cc_s+'\');' %>                

Also, here is my routes file currently: 另外,这是我当前的路线文件:

match '/rktest', :to => 'main#rktest', :via => [:get, :post]

Even this does not work. 即使这样也不起作用。

RestClient.post 'https://runkeeper.com/apps/token', {:params => {
    :grant_type => 'authorization_code',
    :code => @code,
    :client_id => '8e9b36478b764ac38ef1bdabc6d14d60',
    :client_secret => something,
:redirect_uri => "http%3A%2F%2Fopenhealthdesigns.com%2Frktest"}}

I am using respond_to :json at the top of a controller file with respond_with @notes in the method that I want to return the json, and it works well. 我要在控制器文件顶部使用respond_to :json ,并在我想返回json的方法中使用respond_with @notes ,所以效果很好。

The app uses Ajax to return data for an search feature, but I don't think how the data is being called should make a difference. 该应用程序使用Ajax返回搜索功能的数据,但我认为数据的调用方式没有什么不同。

A litte more code for context: 更多有关上下文的代码:

class NotesController < ApplicationController
  require 'coderay'
  respond_to :json                                                                                                                                                                                                         


 def search
  @notes = Note.fulltext_search(params[:title])
  @notes.each do |n|
    n.content = CodeRay.scan(n.content, :ruby).div(:css => :class)
  end
  respond_with @notes
 end

API Docs - depending on how the method is being called. API文档-取决于调用方法的方式。 If using .json, a respond_to block would work also. 如果使用.json,则response_to块也将起作用。

respond_with response_with

respond_to 回应

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM