Zisu
Zisu

Reputation: 497

SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 3 of the JSON data

SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 3 of the JSON data

....trim(b);if(a.JSON&&a.JSON.parse)return a.JSON.parse(b);if(n.test(b.replace(o,"@...

jquery-....min.js (line 2, col 11012)

My code

$(document).ready(function(){
        $('.cls_list_img3').live('click',function(){
            $('footer').after('<div class="error_bg" id="info_message" style="top: 0px; display: block;background:gold"><div class="center_auto"><div class="info_message_text message_area" style="color:black" id="msg_pin">Please Wait....</div><div onclick="return closeNotification()" class="info_close_btn button_area"></div><div class="clearboth"></div></div><div class="info_more_descrption"></div></div>');

             var imgsrc = $(this).attr('rel');
//alert(imgsrc);
            var img = imgsrc.replace('&', '*');
            var img1 = img.replace('&', '*');
            var img2 = img1.replace('&', '*');
            var img3 = img2.replace('&', '*');
            var img4 = img3.replace('&', '*');
             var title = encodeURIComponent($.trim($("#title").val().toString()));
             var body = $(this).children('.body').val();
             var rss_title = $(this).children('.title').val();
             var category = $(this).children('.category').val();
             var rss_link = $(this).children('.rss_link').val();
              var data = "imgsrc=" + img4 + "&source="+rss_link+"&title=" + rss_title+"&des="+body+"&rss_title="+rss_title+"&category="+category;
            // alert(body);
             //alert(rss_title);
               $.ajax({type: "POST", url: "<?php echo base_url() ?>share/pinuploudOneclick",
                                      dataType: 'json',
                                    // async: true,
                                     data: data,
                                     success: function(data)
                {
                    var obj = jQuery.parseJSON(data);
                    //alert(data);
                    alert(obj.message);
                       if(obj.message == 'error'){
                           $('#msg_pin').html('You already uploaded this Pin');
                $('#info_message').css('background','lightpink');

                           }else{
                    $('#msg_pin').html('Pin uploaded successfully');
                    $('#info_message').css('background','lightgreen');
                }
                    setTimeout(function(){ 
                        $('#info_message').remove();
                         }, 3000);
                   // $('#fadeimgoverlay').removeClass('fadeimgoverlay_bg');
                   // $('#fadeimgloading').hide();
                    //$('.event-containernarrow').html(data);
                   // $('.main_container').css('display', 'none');
                    //$('.bd_detail').html(data);
                    //$('body').css('overflow', 'hidden');
                },
                dataType: "html"
            });







            })
        });

Upvotes: 0

Views: 7964

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074028

jQuery parses the JSON for you before calling success because you've specified dataType: 'json'. (If you hadn't, and the server replied with the correct Content-Type, it would have done it anyway.) So you can just use data directly, you don't want to call JSON.parse on it.

So change your success function from

success: function(data)
{
    var obj = jQuery.parseJSON(data); // Don't need this!
    alert(obj.message);
    // ...

to

success: function(obj)                // Just use this directly
{
    alert(obj.message);
    // ...

From the documentation:

success

Type: Function( Anything data, String textStatus, jqXHR jqXHR )

A function to be called if the request succeeds. The function gets passed three arguments: The data returned from the server, formatted according to the dataType parameter or the dataFilter callback function, if specified; a string describing the status; and the jqXHR (in jQuery 1.4.x, XMLHttpRequest) object.

(My emphasis)

By doing it again, you end up calling toString on data, which probably results in either "[object Object]" (if the top level thing is an object) or something else unparseable (if the top level thing is an array, as Array#toString calls Array#join). That's why you get the error.

Upvotes: 3

Related Questions