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.
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.
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.
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.
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 .
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.
****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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With