2

I'm trying to get some json from an action but I have a problem.

I know that you can return "format.js" and return an ".js.erb" file but... Imagine I have an action that return some stuff and I want to work with that in two different ways. I cannot have that two different ways on the same .js.erb because when I return it, both ways will be executed, right? I mean, when I return that file, that file will be processed entirely.

So I tried to use the jQuery way.

I have:

# GET /posts/slug/hidden_content.json
def hidden_content
  @post = Post.find(params[:id])
  @post = hidden_post_part(@post.body)

  respond_to do |format|
    format.json { render json: @post, :status => :ok}
  end
end

And in the controller.coffee:

$ ->
  # Render hidden part of post
  $(".showallpost img").click( ->
    post_url = $(this).next().attr("href").split("/").pop();
    $.ajax
      url: Routes.hidden_content_path(post_url),
      type: "GET",
      contentType: "application/json",
      success: (data) ->
        console.log "foo"
  );

The GET works perfectly and it returns what I need but the success function is never called, I don't see the "foo" log.

I guess that the action should return something to tell to the caller that the GET was successful. (I tried with :status without luck).

So I can't work with my GET data.

Ideas?

Jesus Rodriguez
  • 11,918
  • 9
  • 64
  • 88
  • What about when you try with dataType: "JSON" and remove the contentType option? OR make the request against the same URL, but append .json to it. The issue is that jQuery categorizes this as a failed requests as the response format does not match what it expects it to be or something. – Tanel Suurhans Feb 27 '12 at 13:02
  • @TanelSuurhans I tried dataType and also having the .json in the url, success is not trigged. And Ryan https://github.com/railsware/js-routes Just a js script that helps you with rails routes, handy handy. – Jesus Rodriguez Feb 27 '12 at 13:22

3 Answers3

1

Seems like my controller was returning plain html instead of json, so when I used

dataType: 'json'

I needed to use:

dataType: 'html'

Kinda weird, because it should return some json, but well, it works.

All the thanks to Mike Johnson in: jquery doesn't call success method on $.ajax for rails standard REST DELETE answer

Community
  • 1
  • 1
Jesus Rodriguez
  • 11,918
  • 9
  • 64
  • 88
0

In your js.erb, you could put an

<% if success %>
  some javascript you want to execute on success
<% else %>
  some stuff you want to do on failure
<% end %>
Wilhelm
  • 820
  • 8
  • 10
  • The problem is that I want to use that action from different views and use the data in other ways. That solution doesn't work, that's why I wanted to use ajax in the old way. – Jesus Rodriguez Feb 27 '12 at 14:27
0

I tried a very similar piece of code, and it works -

$ -> 
  # Render hidden part of post
  $(".showallpost").click ->
    #post_url = $(this).next().attr("href").split("/").pop();
    $.ajax
      url: "/posts/1.json", #Routes.hidden_content_path(post_url),
      type: "GET",
      contentType: "application/json",
      success: (data) ->
        console.log "foo"

I suggest you check what url is before the request is sent. It may just get caught up in Jquery's cross-domain request check and not be fired at all - happened to me when I used "localhost:3000/posts/1.json" as url instead of just "posts/1.json"

abhishek
  • 998
  • 6
  • 9