3

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??

Sanjay Rathod
  • 1,083
  • 5
  • 18
  • 45

3 Answers3

3

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).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • but if i use json there is no any response cause it is cross domain – Sanjay Rathod Apr 11 '14 at 13:45
  • If you want to use JSONP, then the server you are requesting the data from has to give you JSONP. See [this question](http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy) for alternatives. – Quentin Apr 11 '14 at 13:47
  • parsererror, Error: jQuery111005023312801262618_1397224574072 was not called above error shows in alert of error function – Sanjay Rathod Apr 11 '14 at 13:57
1

Change

dataType: 'jsonp',

to

dataType: 'json',

Because you are getting JSON, not JSONP, back.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
1

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.

Arman Ortega
  • 3,003
  • 1
  • 30
  • 28