jquery - How to solve SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data in ajax and php -
how solve error: syntaxerror: json.parse: unexpected character @ line 1 column 1 of json data
i sending data , ajax , php.
here 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 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 sample of response data 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 @ line 1 column 1 of json data
i confuse main reason of error, because did same type of coding json earlier.i checked php working fine.please me.thanks.
you returning json
server , parsing html
datatype in client side . so, in code change datatype
dattype: 'html'
to
datatype: 'json',
hope helps .
Comments
Post a Comment