Access instance variable in ajax rendered by controller - Rails

I am making an ajax call to my controller. Controller is sending in the
response in an instance variable(as a json object). How can I access the
same in my ajax javascript?

This is my ajax call code:

$(document).ready(function(){
var currentCellText;
$(".inline").click(function() {
currentCellText = $(this).text();
$.ajax({
type: 'GET',
dataType: "json",
url:'/test',
async: false,
data:{ foo1:currentCellText
},
dataType: "json",
success:function(data){
alert('<%= @response %>');
},
error:function(data){
alert("Error");
}
});
});
});

This is my controller code.

def some_action
# after assigning a value to variable response
@response = Response.where(some condition)
respond_to do |format|
format.html #this will simply render the view
format.json { render :json => { :response => @response } }
end
end

I am getting an empty square bracket as my output. I am not able to find
out where I am wrong. Please guide me through.

nikhil rn wrote in post #1101054:

I am making an ajax call to my controller. Controller is sending in the
response in an instance variable(as a json object). How can I access the
same in my ajax javascript?

$(document).ready(function(){
  var currentCellText;
  $(".inline").click(function() {
    currentCellText = $(this).text();
    $.ajax({
      type: 'GET',
      dataType: "json",
      url:'/test',
      async: false,
      data:{ foo1:currentCellText
      },
      dataType: "json",
      success:function(data){
        alert('<%= @response %>');
      },
      error:function(data){
        alert("Error");
      }
    });
  });
});

I am getting an empty square bracket as my output. I am not able to find
out where I am wrong. Please guide me through.

Have you confirmed for certain that your action method is returning the
JSON you expect?

$ curl http://localhost:3000/test.json

Hello Robert Walker.

After some operation in the controller, I am assigning a value to the
variable. When I print it in the controller using a "puts", I am getting
the right output. But when I render it to the view, I am NOT getting the
required json alert in the javascript. Where am I going wrong? Can you
please guide me?

Nikhil

Am I trying to send it wrong from the controller? I mean, is my syntax
wrong in sending the JSON from controller to the view? I have used such
a JSON in other methods in the controller. I have no issues there. But I
am not able to understand why I am not able to pass the same to the view
using respond_to in controller.

Nikhil

success:function(data){
alert(data);

}

Now I am able to pass the required json properly. How do I alert the
same in javascript.

Nikhil

If I print the json object value from the controller, I am getting the
required output. But if I alert the same in the ajax, my output is as
below:

[object Object]

Nikhil

alert(object) calls object.toString() which returns [object Object] for any object. If you want to dump the contents of the object, loop through it and print each key-value pair.

My json data printed at the controller is correct. But my data.length at
the ajax javascript says "undefined". Even if i loop, it doesnt enter
the loop as data.length is undefined.

success(data){
var x = new Array();
alert("created new array");
for (var i=0;i<data.length;i++)
{
x[i] ,x[data[i].toString()]=function(){};
}
}

Nikhil