[英]POST JSON response to HTTP request in Ruby
我正在Heroku上運行Ruby應用程序。 該應用程序返回一個JSON,當我轉到瀏覽器的調試器時可以訪問該JSON。 JSON響應具有以下模板:
rates = {
"Aluminium" => price[1],
"Copper" => price_cu[1],
"Lead" => price_pb[1],
"Nickel" => price_ni[1],
"Tin" => price_sn[1],
"Zinc" => price_zn[1],
}
樣本回復:
{
"Aluminium":"1765.50",
"Copper":"7379.00",
"Lead":"2175.00",
"Nickel":"14590.00",
"Tin":"22375.00",
"Zinc":"2067.00"
}
我為實現此目的而編寫的代碼是:
Test.rb
class FooRunner
def self.run!
#calculations_for_rates
rates.to_json
end
if __FILE__ == $0
puts FooRunner.run!
end
app.rb
require 'sinatra'
require './test.rb'
result = FooRunner.run!
File.open('output.json','w') do |f|
f.write result
end
content_type :json
result
當我嘗試使用
$.getJSON('app-url',function(data){
console.log(data);
});
這給我一個錯誤的說法
No 'Access-Control-Allow-Origin' header is present on the requested resource.
我有辦法通過將JSON寫入HTTP響應來直接訪問JSON響應嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.