开发者

Simply returning success or failure from ajax call in rails

I have a little ajax call that calls rails:

    $.ajax({
        type: "POST",
        url: '...',
        data: ({    ...
                }),
        success:    function(response, status) {
                    console.log(status);
        }
     });

In the rails controller I'm simpl开发者_Python百科y deleting an entry from the database, and I simply want to return if it was successful or not. What's the best way?

Should I just return JSON in respond_to? If so, what exactly would you have it contain?


Best way to signify success in this way is to put the following in your controller...

def destroy
  # ... your code ...
  respond_to do |format|
    format.json { head :ok }
  end
end


try this it's working for me

def destroy 
  ...        
  render json: {}, status: 200
end


I found this shorter way to do the job:

def destroy
  # ... your code ...
  head :ok # this will return HTTP 200 OK to jQuery!
end


When you execute your query it might be returning some code that says it executed succesfully to confirm that row was deleted. So you can return that just to make sure query was also executed successfully along with the ajax call.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜