Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data in ajax and php

Tags:

jquery

ajax

php

How to solve this error: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

i was sending some data from and to ajax and php.

here is my ajax code:

flag = 111;
var dt = $(this).serializeArray();
dt.push({
  name: 'flag',
  value: flag
});

$.ajax({
  url: 'emp.php',
  type: "post",
  async: true,
  data: dt,
  dataType: 'html',
  contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
  success: function(data) {
    var x = JSON.parse(data); //THIS Line shows error!!
    alert(x);
    $('#name').val(x.ename);
    $('#designation').val(x.designation);
    $('#department').val(x.department);
    $('#sd').val(x.secdivision);
  },
  error: function(jqXHR, textStatus, errorThrown) {
    console.log(textStatus, errorThrown);
  }
});

here is my php:

$empid = (isset($_POST['employeeid'])) ? $_POST['employeeid'] : 'NOT';
$flag  = (isset($_POST['flag'])) ? $_POST['flag'] : 0;
if($flag == 111){
    $stid = oci_parse($conn, " begin   
                       :result :=  PKG_PAYROLL.get_emp_by_id('<employee_id>$empid/employee_id>');  
                       end;" ); 

    oci_bind_by_name($stid, ':result',$ru, 5000);   
    $output = oci_execute($stid);
    $ru = new SimpleXMLElement($ru);    
    $json = json_encode($ru, JSON_NUMERIC_CHECK);
    $jsonarray = json_decode($json ,true);  
    $jsn = $jsonarray['employee'];

    $array = array('employee' =>   $jsn['EMPID'],
                       'ename' => $jsn['ENAME'],
                       'designation' => $jsn['DESIGNATION'], 
                       'department'=>  $jsn['DEPARTMENT'],
                       'secdivision'=>  $jsn['SECDIVISION']);                      
    echo json_encode($array);
}

Updates: Here is a sample of response data i got in console after echo json_encode($array);

<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding
='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f
; font-size: x-large;'>( ! )</span> Notice: Undefined index: employee in C:\wamp\www\Payroll\emp.php
 on line <i>24</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align
='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left'
 bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0002</td><td bgcolor
='#eeeeec' align='right'>247040</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\Payroll
\emp.php' bgcolor='#eeeeec'>..\emp.php<b>:</b>0</td></tr>
</table></font>
{"employee":"FMCSC00015","ename":"Tom","designation":"Teacher","department":"English","secdivision":"Academic"
}

parsererror SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

I am confuse about the main reason of this error, because i already did same type of coding with json earlier. I checked that php is working fine.

like image 350
user5005768Himadree Avatar asked Feb 06 '16 04:02

user5005768Himadree


People also ask

How do I fix SyntaxError JSON parse unexpected character at line 1 column 1 of the JSON data?

The "SyntaxError: JSON. parse: unexpected character" error occurs when passing a value that is not a valid JSON string to the JSON. parse method, e.g. a native JavaScript object. To solve the error, make sure to only pass valid JSON strings to the JSON.

What does JSON parse unexpected character at line 1 column 1 of the JSON data mean?

If you encounter the following error. Error parsing JSON: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data. it means that instead of JSON response from LiveAgent API, the client application in browser received something that is not JSON.

How do I fix unexpected token in JSON error?

The "Unexpected token u in JSON at position 0" error occurs when we pass an undefined value to the JSON. parse or $. parseJSON methods. To solve the error, inspect the value you're trying to parse and make sure it's a valid JSON string before parsing it.

What is JSON () method?

The json() method of the Response interface takes a Response stream and reads it to completion. It returns a promise which resolves with the result of parsing the body text as JSON .


2 Answers

You are returning JSON from server and parsing HTML dataType in client side. So, in your code change your datatype:

dataType: 'html'

to

dataType: 'json'

Hope this helps.

like image 164
Drudge Rajen Avatar answered Oct 20 '22 09:10

Drudge Rajen


****If your response is in HTML format, but response contains json data . Then you have occured this error(' JSON.parse: unexpected character at line 1 column 1 of the JSON data'). To avoid this error you have use this code written below:****

$.ajax({
       url: 'emp.php',
       type: "post",
       async: true,
       data: dt,
       dataType: 'html',
       contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
       success: function(data) {
       try {
         var x = JSON.parse(data);
       } catch (e) {
           return false;
       }
         //JSON.parse(data) THIS Line shows error!!
       alert(x);
       $('#name').val(x.ename);
       $('#designation').val(x.designation);
       $('#department').val(x.department);
       $('#sd').val(x.secdivision);
      },
    error: function(jqXHR, textStatus, errorThrown) {
       console.log(textStatus, errorThrown);
    }
  });

If you response from PHP in simple json format then you have use this code.But in this case your response from PHP file is only json format.

$.ajax({
   url: 'emp.php',
   type: "post",
   async: true,
   data: dt,
   dataType: 'json',
   contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
   success: function(data) {
   $('#name').val(data.ename);
   $('#designation').val(data.designation);
   $('#department').val(data.department);
   $('#sd').val(data.secdivision);
  },
  error: function(jqXHR, textStatus, errorThrown) {
   console.log(textStatus, errorThrown);
}
like image 29
Sandeep K. Avatar answered Oct 20 '22 09:10

Sandeep K.