gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 13:13:03 25 4
gpt4 key购买 nike

如何解决这个错误: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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com