0

I have a rails app with 2 models namely a security and a stock_quote model

security.rb

has_many :stock_quotes, dependent: :destroy

stock_quotes.rb

belongs_to :security, class_name: "Security", foreign_key: 'security_id'

I have used rabl to create a JSON API to display a json of securities and their respective stock_quotes. The format is as follows:

security/show.html.rabl

object @security
attributes :id, :security, :security_code, :category

node(:edit_url){ |security| edit_security_url(security)}

child :stock_quotes do 
    attributes :stock_quote_id, :security_id, :yesterday, :current, :price_change, :percentage_change, :high, :low, :published_at
end

security/index.html.erb

collection @securities
extends "securities/show"

The API was successfully created and when I visit /securities.json I get the data that is in the database in the following format

[

{
    "security": {
        "id": 1,
        "security": "Google",
        "security_code": null,
        "category": null,
        "stock_quotes": [
            {
                "stock_quote": {
                    "security_id": 1,
                    "yesterday": 8.6,
                    "current": 9.45,
                    "price_change": 0.85,
                    "percentage_change": 9.88,
                    "high": 9.45,
                    "low": 9.45,
                    "published_at": "2014-05-29T08:04:54.000Z"
                }
            }
        ]
    }
},
{
    "security": {
        "id": 2,
        "security": "Facebook",
        "security_code": null,
        "category": null,
        "stock_quotes": [
            {
                "stock_quote": {
                    "security_id": 2,
                    "yesterday": 30,
                    "current": 31.75,
                    "price_change": 1.75,
                    "percentage_change": 5.83,
                    "high": 31.75,
                    "low": 30.75,
                    "published_at": "2014-05-29T08:04:54.000Z"
                }
            }
        ]
    }
}

]

I however cannot work with this data. I have even tried to log it to the console to no avail and it returns no error on the firebug console.Here is a simple script in my securities/index.html.erb file that I'm using to log the data to the console.

<div id="sec">
<script type="text/javascript">
  var url = "/securities.json";
  var stock = {};
  $.ajax({
   url: url,
   cache: false,
   dataType: 'jsonp', //will set cache to false by default
   context: stock,
   success: function(data){
    console.log(data);
   }
  });
</script>
</div>

After visiting the /securities page, and checking my firebug console, I see that a get request to /securities.json has been performed but however my data is not being logged to the console as I have stated in my code.

How do I get this data to get logged to the console, since I'm sure that if I am able to log it on the console,using that data for charting which is what I intend to, will not be a problem.

Mutuma
  • 1,943
  • 3
  • 24
  • 33
  • 1
    How about if you use `complete: function [...]` instead of `success: function [...]` in your ajax call? – Damien Roche Jun 02 '14 at 23:18
  • from [this](http://stackoverflow.com/questions/5240876/difference-between-success-and-complete), .success() only gets called if your webserver responds with a 200 OK HTTP header while .complete() will always get called no matter if the ajax call was successful or not .. so what may be the cause of my ajax call not being successful in this case? – Mutuma Jun 02 '14 at 23:24
  • 1
    Great to hear! But as @Thib mentioned, changing to `json` type should work. You can see why `jsonp` isn't triggering `success` here: http://stackoverflow.com/questions/17748647/ajax-request-returns-200ok-but-fails-despite-return-data-being-json – Damien Roche Jun 02 '14 at 23:32
  • Thanks. Got it. Tried changing to json and it worked perfectly but I've figured out why it jsonp didn't work – Mutuma Jun 02 '14 at 23:41

0 Answers0