0

I am currently trying to navigate to send a GET request to a URL, parse what is returned into a JSON and use it in my Javascript.

At the moment, I am using dummy data like so :

data = [
{
:id=> "1",
:rev=> "1.0",
:text=> "This is a test"
}]

And this works perfectly in my code :

<ul>
  <% data.each do |item| %>
  <li>
    ...

Now I have a webpage which returns the data after a GET request in this form :

{"alerts":[{"_id":"1","_rev":"1.0","text":"This is a test"}]}

when I call this, however, I am getting a connection refused error, here is my code :

# require 'open-uri'
# content = open("http://mywebsite.returnsDataAbove.com").read

and

# require 'net/http'
# require 'json'

# url = URI.parse('http://mywebsite.returnsDataAbove.com')
# req = Net::HTTP::Get.new(url.to_s)
# res = Net::HTTP.start(url.host, url.port) {|http|
  # http.request(req)
# }
# puts res.body
# hash = JSON.parse res.body
# puts hash

what have I done incorrectly here?

Thanks a lot.

8
  • error information would be helpful, is not clear in your question what leads to what Commented Sep 17, 2014 at 16:06
  • mostly the last part, you write you have a webpage that returns the data, then you don't Commented Sep 17, 2014 at 16:07
  • @juanpastas if I navigate to the page using firefox data is returned, calling it from another webpage does not work Commented Sep 17, 2014 at 16:14
  • hmmm can't understand your redaction Commented Sep 17, 2014 at 16:17
  • @juanpastas If I go to mywebsite.returnsDataAbove.com via a normal browser call (i.e in Firefox) it will work and print the information on the screen. Calling my code above, however, results in a "connection refused" error being displayed on my screen. I am not sure how to debug this or provide anymore information - any help in this would be appreciated. Thank you very much :) Commented Sep 17, 2014 at 16:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.