php 如何解决SyntaxError:JSON.parse:ajax和php中JSON数据第1行第1列的意外字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35237388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to solve SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data in ajax and php
提问by user5005768Himadree
How to solve this error: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
如何解决此错误: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
i was sending some data from and to ajax and php.
我正在向 ajax 和 php 发送一些数据。
here is my ajax code:
这是我的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);
}
});
here is my php:
这是我的 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 is a sample of response data i got in console after 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
parsererror SyntaxError: JSON.parse: JSON 数据第 1 行第 1 列的意外字符
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 进行了相同类型的编码。我检查了 php 工作正常。
回答by Drudge Rajen
You are returning JSON
from server and parsing HTML
dataType in client side. So, in your code change your datatype:
您正在JSON
从服务器返回并HTML
在客户端解析dataType。因此,在您的代码中更改您的数据类型:
dataType: 'html'
to
到
dataType: 'json'
Hope this helps.
希望这可以帮助。
回答by Sandeep K.
****If your response is in HTML format, but response contains json data . Then you have occured this error(' JSON.parse: unexpected character at line 1 column 1 of the JSON data'). To avoid this error you have use this code written below:****
****如果您的响应是 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);
}
});
If you response from PHP in simple json format then you have use this code.But in this case your response from PHP file is only json format.
如果您以简单的 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);
}