【问题标题】:How to solve SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data in ajax and php如何解决 SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data in ajax and php
【发布时间】:2016-05-16 05:04:47
【问题描述】:

如何解决这个错误:SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

我正在向 ajax 和 php 发送一些数据。

这是我的 ajax 代码:

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 SyntaxError: JSON.parse: 第 1 行出现意外字符 JSON数据的第1列

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

【问题讨论】:

  • 在将数组编码为 json 之前,请查看 Network 选项卡以查看数组返回的内容
  • 谢谢@Won Jun Bae。哪个文件检查网络选项卡 php 或 ajax?谢谢
  • 什么if($flag != 111){?还是有什么错误?
  • 您正在从服务器返回 json 并在客户端解析“HTML”数据类型。
  • 你可以在 Chrome 上使用 Inspector

标签: php jquery ajax


【解决方案1】:

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

dataType: 'html'

dataType: 'json'

希望这会有所帮助。

【讨论】:

  • 可以在将数组编码为 json 之前打印并发布到这里吗??
  • 说真的??它是 html 人。 JSON 解析器只会解析 json 数据。您正在尝试使用 json parser 解析 html。看看api.jquery.com/jquery.parsejson json 解析器实际上做了什么。
  • 我什至不知道你到底想做什么。
  • 我的快乐伙伴。请接受答案,以便对其他人也有帮助。
  • 您正在从 php 发送 html 数据,但在您的 ajax 调用中解析 json。 Json 只解析 json 数据。
【解决方案2】:

****如果您的响应是 HTML 格式,但响应包含 json 数据。然后你发生了这个错误('JSON.parse:JSON 数据的第 1 行第 1 列的意外字符')。为避免此错误,您已使用以下编写的代码:****

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

如果您以简单的 json 格式从 PHP 响应,那么您已经使用了此代码。但在这种情况下,您从 PHP 文件的响应仅是 json 格式。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-05
    • 1970-01-01
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    • 2019-01-31
    相关资源
    最近更新 更多