Alex Rivera | Logout

jQuery Ajax GET JSON

Asked 2012-09-07T16:27:26.680
16

I have this piece of code:

$.ajax({
url: '/piece.json',
type: "GET",
dataType: "json",
success: function (data) {
    alert(data);
}
});

I am on a rail3 app, when I type "/piece.json" I get on my browser what I want. But I cant make it alert using Javascript! Why this happens?

Solved, had to use this:

complete: function (data_response) {

    alert(data_response.responseText);
},
Edit
Report

1 Answer

3

Presumably you don't have a working route for /piece.json. Use the Developer Tools in your browser and log from the rails app to see what is going on with the XHR request.

(Look at the http response in the browser.)

Note that you can more simply use:

$.get('/piece.json',function(data){
  // data is a JS object parsed from a JSON response
},'json');
answered 2012-09-07T16:31:55.230

Your Answer