i have some code in ajax call like below
$.ajax({
url: query_string,
cache: true,
type: 'GET',
async: false, // must be set to false
success: function (data, success) {
alert(jquery.parseJSON(data));
alert('success');
//alert(data);
//alert(success);
},
dataType: 'jsonp',
error :function( jqxhr, textStatus, error ) {
var err = textStatus + ', ' + error;
alert(err);
},
plete: function (jqxhr, textStatus ){
//alert( "plete: " + JSON.stringify(jqxhr)+" "+ textStatus );
}
});
when i run this code into firefox with fire bug. firebug shows response in perect json format but shows following error in firebug
SyntaxError: missing ; before statement
{"products":[{"title":"xyz","id":1718,"created_at
so how can i solve it??
i have some code in ajax call like below
$.ajax({
url: query_string,
cache: true,
type: 'GET',
async: false, // must be set to false
success: function (data, success) {
alert(jquery.parseJSON(data));
alert('success');
//alert(data);
//alert(success);
},
dataType: 'jsonp',
error :function( jqxhr, textStatus, error ) {
var err = textStatus + ', ' + error;
alert(err);
},
plete: function (jqxhr, textStatus ){
//alert( "plete: " + JSON.stringify(jqxhr)+" "+ textStatus );
}
});
when i run this code into firefox with fire bug. firebug shows response in perect json format but shows following error in firebug
SyntaxError: missing ; before statement
{"products":[{"title":"xyz","id":1718,"created_at
so how can i solve it??
Share Improve this question edited Apr 11, 2014 at 16:22 Sanjay Rathod asked Apr 11, 2014 at 13:41 Sanjay RathodSanjay Rathod 1,1435 gold badges18 silver badges48 bronze badges 3- 1 Duplicate of stackoverflow./questions/19456146/… – Jay Blanchard Commented Apr 11, 2014 at 13:43
-
The response data is parsed for you. Get rid of
jquery.parseJSON
and justalert(data)
– Reinstate Monica Cellio Commented Apr 11, 2014 at 13:46 - parsererror, Error: jQuery111005023312801262618_1397224574072 was not called above error shows in alert of error function – Sanjay Rathod Commented Apr 11, 2014 at 13:58
3 Answers
Reset to default 3You are telling jQuery to process the response as JSONP:
dataType: 'jsonp'
… but the response is JSON, not JSONP.
Get rid of the p
or get rid of the dataType
line entirely and let jQuery determine the data type from the Content-Type
of the response (which should be application/json
).
Change
dataType: 'jsonp',
to
dataType: 'json',
Because you are getting JSON, not JSONP, back.
In your controller make sure that you render the format correctly. example:
def view_product
data = Product.find params[:id]
render :json => data, :callback => params[:callback]
end
In your render method, there must have the :callback parameter otherwise it will render in json instead of jsonp.