how to solve json error : SyntaxError: missing ; before statement {"products":[{"title":"xyz","id":1718,"created_at?
12,777
Solution 1
You 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
).
Solution 2
Change
dataType: 'jsonp',
to
dataType: 'json',
Because you are getting JSON, not JSONP, back.
Solution 3
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.
Related videos on Youtube
Author by
Sanjay Rathod
Updated on June 11, 2022Comments
-
Sanjay Rathod almost 2 years
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); }, complete: function (jqxhr, textStatus ){ //alert( "complete: " + 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??
-
Jay Blanchard about 10 yearsDuplicate of stackoverflow.com/questions/19456146/…
-
-
Sanjay Rathod about 10 yearsbut if i use json there is no any response cause it is cross domain
-
Quentin about 10 yearsIf you want to use JSONP, then the server you are requesting the data from has to give you JSONP. See this question for alternatives.
-
Sanjay Rathod about 10 yearsparsererror, Error: jQuery111005023312801262618_1397224574072 was not called above error shows in alert of error function
-
Pramod Solanky over 9 yearsYes, you're right on this. You must also disable
protect_from_forgery
on that action to make it work.