我正在向网站发送http请求,我想从响应中存储div的值。
这就是我在控制器中所做的事情:
class RoomsController < ApplicationController
require "uri"
require "net/http"
require 'json'
require 'cgi'
def test
dialect = params[:dialect]
text = params[:text]
uri = URI('http://www.degraeve.com/cgi-bin/babel.cgi')
params = { :w => text, :d => dialect }
uri.query = URI.encode_www_form(params)
res = Net::HTTP.get_response(uri)
data = res if res.is_a?(Net::HTTPSuccess)
render json: data.body
end
end
我使用GET方法传递2个参数来获取响应。但我只需要一个div的内容。我该怎么做?
答案 0 :(得分:0)
我这样做了:
dialect = current_user.dialect.name
text = params[:text]
uri = URI('http://www.degraeve.com/cgi-bin/babel.cgi')
params = { :w => text, :d => dialect }
uri.query = URI.encode_www_form(params)
response = Net::HTTP.get_response(uri)
render inline: response.body.html_safe, layout: false if response.is_a?(Net::HTTPSuccess)
然后在.js文件中获得了我想要的div。