0

There is a service that when I hit it it return me JSON like this, so I think it is returning me the correct JSON I need:

enter image description here

Now in my client contoller I have something simple as this to call the Service and get the JSON back and if I put break points I can see that yes it does have the same JSON posted above

class PharmacyController < ApplicationController
  def index
    @order_summary = client.get_order_summary(10)

    gon.data = @order_summary # my attempt to pass it with GON gem.
  end
end

and then in my pharmacy/index.html.erb I am calling my Javascript:

<%= javascript_include_tag 'dummy.js' %>

and my dummy.js javascript file looks like this for now, just to make sure I can see the same number of elements no the array:

$( document ).ready(function() {
    var data = gon.data;
    console.log( data.length);
});

but what it returns is "undefined"

Do you have any other suggestions for how to achieve this?

1
  • 1
    Instead of vanilla javascript or jquery as noted in an answer below, consider using AngularJS or EmberJS. Also if your Rails controller really isn't helping that much, you could just call the service directly from Javascript (AngularJS, EmberJS, etc.) vs. having to hit the controller just to hit the other service, only if you can do that securely, of course. Commented May 21, 2013 at 17:07

2 Answers 2

1

Use jQuery Ajax

$.ajax({
        url: <url>,
        type: 'get',
        data: {},
        success: function (data) {
            console.log( data);
        }
    });
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, is there some weird semicolons missing? That's what I have so far:"$( document ).ready(function() { $.ajax({ url: <url>, type: 'get', data: {}, success: function (data) { console.log(data); }; }); });"
$(document).ready(function() { $.ajax({ url: <url > , type: 'get', data: {}, success: function(data) { console.log(data); } }); });
0

I just should have done:

gon.data.body

.body was missing.

Comments

Your Answer

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