php - 如何解决 SyntaxError : JSON. parse: unexpected character at line 1 column 1 of the JSON data in ajax and php

标签 php jquery ajax

如何解决这个错误:SyntaxError: JSON.parse: unexpected character at the 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: unexpected character at line 1 column 1 of the JSON data

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

最佳答案

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

dataType: 'html'

dataType: 'json'

希望这对您有所帮助。

关于php - 如何解决 SyntaxError : JSON. parse: unexpected character at line 1 column 1 of the JSON data in ajax and php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35237388/

相关文章:

javascript - 标记 event.listener 迭代问题,Laravel

php - 在 Symfony2 中包含用户自定义函数的最佳实践

php - 为什么我的 HTML 单选按钮会检查最后一个选项,而不管选择了什么选项?

php - 扩展 PHP/AJAX 轮询网站更改的有效方法

javascript - 使用 jQuery 解析 XML

php - 为什么需要 TLS/SSL?

php - 如何将 CGRIDVIEW 中生成的表包含到 YII 中的 <DIV>

jquery - 如何编写脚本来转到上一页和下一页?

jquery - 如何添加jquery ajax返回数据是否为空的判断?

javascript - 如何从 PHP 文件获取 ajax 结果值