如何解决SyntaxError:JSON.parse:Ajax和php中JSON数据的第1行第1列的意外字符 [英] How to solve SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data in ajax and php

查看:86
本文介绍了如何解决SyntaxError:JSON.parse:Ajax和php中JSON数据的第1行第1列的意外字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解决此错误: SyntaxError:JSON.parse:JSON数据第1行第1列的意外字符

我正在从ajax和php发送一些数据.

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

这是我的Ajax代码:

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);
  }
});

这是我的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);
}

更新:这是我在 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语法错误:JSON.parse:第1行出现意外字符JSON数据的第1列

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

我对该错误的主要原因感到困惑,因为我之前已经使用json进行过相同类型的编码.我检查了php是否工作正常.

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.

推荐答案

您正在从服务器返回 JSON 并在客户端解析 HTML dataType.因此,在您的代码中更改您的数据类型:

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

dataType: 'html'

dataType: 'json'

希望这会有所帮助.

这篇关于如何解决SyntaxError:JSON.parse:Ajax和php中JSON数据的第1行第1列的意外字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆