最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to solve json error : SyntaxError: missing ; before statement {"products":[{"title&a

programmeradmin1浏览0评论

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 just alert(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
Add a ment  | 

3 Answers 3

Reset to default 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).

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论