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

Puneet Arora
Puneet Arora used Ask the Experts™
on
Here is my javascript code

<script type="text/javascript">
      
$(document).ready(function(){
	 $(location).attr('href');
     
		$('#myddl1').change(function(){
			 
  		var fk_i_id = $('#myddl1').val().trim();
		if(fk_i_id != 0)
		{ 
				alert(fk_i_id);
			 	
			$.ajax({
				 type:'POST',
				 url:'http://localhost/data/performa/index.php/Testing/getCatB?id='+fk_i_id,
				 datatype: 'json',
				  data: {id:fk_i_id},
				  
				  success: function(data){
				  var opts = $.parseJSON(data);
					alert(data);
				  
				  
				 $('#myddl2').empty();
				$.each(opts, function(i, d) {

					  $('#myddl2').append('<option value="' + d.pk_i_sub_id + '">' + d.s_name + '</option>');
                });


				
				} 

			 
         



			});
		}
	})
})
        



            
            </script>
  

 

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
RobOwner (Aidellio)
Most Valuable Expert 2015
Commented:
Two issues:

1) Need to see your JSON from http://localhost/data/performa/index.php/Testing/getCatB?id='+fk_i_id to make sure it's compliant as the error you're getting would indicate otherwise
2) You're sending the id twice, once in the querystring and again as form posted data.  Do you need both?
Expert of the Year 2008
Top Expert 2008
Commented:
>> datatype: 'json',
Javascript is case-sensitive.  It should be capital 'T':
dataType:'json',

>> var opts = $.parseJSON(data);
There should not any need to call $.parseJSON().  It should already be converted to JSON object automatically (since you already specified dataType:'json').
RobOwner (Aidellio)
Most Valuable Expert 2015

Commented:
Well spotted hielo :)
Founder
Commented:
I need to post multiple params . How many ! will depend on user's selection .
Infact , its working now , thanks for corrections suggested ! , Unwanted non-whitespace character
was coming due to some value in database row . It is solved now
Puneet AroraFounder

Author

Commented:
Found the solution after  study of the data coming from database

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial