Javascript 通过JS AJAX调用调用Rails控制器数据

Javascript 通过JS AJAX调用调用Rails控制器数据,javascript,jquery,ruby-on-rails,ajax,api,Javascript,Jquery,Ruby On Rails,Ajax,Api,我正在制作一个rails应用程序,在控制器中使用此代码调用API-我首先调用库存端点,然后分别调用其他两个id端点存储id、产品id,以获取链接到库存的特定数据段。此数据被传递到一个散列中,该散列成为“@inventory/transformed results”: class InventoriesController < ApplicationController def index response = Typhoeus.get("http://lcboapi.com/inven

我正在制作一个rails应用程序,在控制器中使用此代码调用API-我首先调用
库存端点
,然后分别调用其他两个id端点
存储id、产品id
,以获取链接到库存的特定数据段。此数据被传递到一个散列中,该散列成为“@inventory/transformed results”:

class InventoriesController < ApplicationController
 def index
 response = Typhoeus.get("http://lcboapi.com/inventories")
 parsed_json = JSON.parse(response.body)

transformed_results = []

parsed_json["result"].each do |inventory|
  transformed_results.push(
    {
      product_name: product_lookup(inventory["product_id"]),
      store_name: store_lookup(inventory["store_id"]),
      quantity: inventory["quantity"],
    }
  )
end

@inventories = transformed_results
end
  private

  def store_lookup(store_id)
  response = Typhoeus.get("http://lcboapi.com/stores/#{store_id}")
    parsed_json = JSON.parse(response.body)
  return parsed_json["result"]["name"]
end

 def product_lookup(product_id)
 response = Typhoeus.get("http://lcboapi.com/products/#{product_id}")
     parsed_json = JSON.parse(response.body)
 return parsed_json["result"]["name"]
 end
end
在一个js文件中(在
assets/javascript
中),您需要执行以下操作:

storeLookupResults = $.ajax({
  url: "/inventories.js",
  type: 'GET'
})
storeLookupResults.success (data) =>
  # do stuff with your results
  # 'data' will contain your json
def index
  response = Typhoeus.get("http://lcboapi.com/inventories")
  parsed_json = JSON.parse(response.body).with_indifferent_access

  @inventories = parsed_json[:result].map do |inventory|
    {
      product_name: product_lookup(inventory[:product_id]),
      store_name: store_lookup(inventory[:store_id]),
      quantity: inventory[:quantity],
    }
  end

  respond_to do |format|
    format.html
    format.json {render json: @inventories, status: :ok}
  end  

end
注意:我制定了路线,所以你需要确保你使用的是真正的路线

然后,在
InventoriesController
中,将
索引修改为类似以下内容:

storeLookupResults = $.ajax({
  url: "/inventories.js",
  type: 'GET'
})
storeLookupResults.success (data) =>
  # do stuff with your results
  # 'data' will contain your json
def index
  response = Typhoeus.get("http://lcboapi.com/inventories")
  parsed_json = JSON.parse(response.body).with_indifferent_access

  @inventories = parsed_json[:result].map do |inventory|
    {
      product_name: product_lookup(inventory[:product_id]),
      store_name: store_lookup(inventory[:store_id]),
      quantity: inventory[:quantity],
    }
  end

  respond_to do |format|
    format.html
    format.json {render json: @inventories, status: :ok}
  end  

end
请注意,
.map
返回一个
数组
。因此,您不必这样做:

transformed_results = []

parsed_json["result"].each do |inventory|
  transformed_results.push(
    {
      product_name: product_lookup(inventory["product_id"]),
      store_name: store_lookup(inventory["store_id"]),
      quantity: inventory["quantity"],
    }
  )
end    

@inventories = transformed_results
还请注意,我确实:

parsed_json = JSON.parse(response.body).with_indifferent_access
这纯粹是一种风格偏好。我喜欢使用符号而不是字符串。

在一个js文件(在
资产/javascript
中)中,您需要执行以下操作:

storeLookupResults = $.ajax({
  url: "/inventories.js",
  type: 'GET'
})
storeLookupResults.success (data) =>
  # do stuff with your results
  # 'data' will contain your json
def index
  response = Typhoeus.get("http://lcboapi.com/inventories")
  parsed_json = JSON.parse(response.body).with_indifferent_access

  @inventories = parsed_json[:result].map do |inventory|
    {
      product_name: product_lookup(inventory[:product_id]),
      store_name: store_lookup(inventory[:store_id]),
      quantity: inventory[:quantity],
    }
  end

  respond_to do |format|
    format.html
    format.json {render json: @inventories, status: :ok}
  end  

end
注意:我制定了路线,所以你需要确保你使用的是真正的路线

然后,在
InventoriesController
中,将
索引修改为类似以下内容:

storeLookupResults = $.ajax({
  url: "/inventories.js",
  type: 'GET'
})
storeLookupResults.success (data) =>
  # do stuff with your results
  # 'data' will contain your json
def index
  response = Typhoeus.get("http://lcboapi.com/inventories")
  parsed_json = JSON.parse(response.body).with_indifferent_access

  @inventories = parsed_json[:result].map do |inventory|
    {
      product_name: product_lookup(inventory[:product_id]),
      store_name: store_lookup(inventory[:store_id]),
      quantity: inventory[:quantity],
    }
  end

  respond_to do |format|
    format.html
    format.json {render json: @inventories, status: :ok}
  end  

end
请注意,
.map
返回一个
数组
。因此,您不必这样做:

transformed_results = []

parsed_json["result"].each do |inventory|
  transformed_results.push(
    {
      product_name: product_lookup(inventory["product_id"]),
      store_name: store_lookup(inventory["store_id"]),
      quantity: inventory["quantity"],
    }
  )
end    

@inventories = transformed_results
还请注意,我确实:

parsed_json = JSON.parse(response.body).with_indifferent_access

这纯粹是一种风格偏好。我喜欢用符号而不是字符串。

好的,但那是;是从不同端点传入相关数据的私有方法。难道我不能通过ajax获得我的
转换的\u结果
散列吗?除了在ajax调用中呈现完整的html脚本(而不是json)之外,这很有效!好,但是,;是从不同端点传入相关数据的私有方法。难道我不能通过ajax获得我的
转换的\u结果
散列吗?除了在ajax调用中呈现完整的html脚本(而不是json)之外,这很有效!