Reputation: 2341
I am using jQuery ajax method to get response from my server. Here's my code:
function getText(id){
$.ajax({
type: 'GET',
url: '/myurl/' + id + '/edit.js',
data: {'id': id},
success: function(data){
alert(data);
},
dataType: 'json'
});
}
I am using ruby on rails, and on my rails controller, I am sending some text as json response. When I check in the firebug, the data is returned correctly. But I don't get anything with the alert(data); that I have called upon success. Can anyone tell me what's wrong here? Thanks.
Upvotes: 0
Views: 2837
Reputation: 14501
dataType: 'json'
specifies that you are receiving json data from the Ajax call. You're treating that data
as if it were plain text. If you are, in fact, receiving plain text, then you need to set dataType: 'text'
. If you're receiving HTML, then specify dataType: 'html'
Upvotes: 2
Reputation: 6981
In the alert does it same something like Object object? It should as the data is now a JavaScript object. If you want to see your data, look at the properties, e.g. alert(data.someProperty)
.
Upvotes: 1