我正在尝试使用ajax请求获取数据,以下是ajax请求。
document.getElementById("myForm").style.display = "block";
$('body').css('overflow','hidden');
$.ajax({
type: 'POST',
url: 'techtest.php',
dataType: 'json',
data: { id: val },
success: function(response, status, xhr){
console.log(response.array[1]);
document.getElementById('tech_about_me').innerHTML = response.array[0];
document.getElementById('tech_assest_group').innerHTML = response.array[1];
document.getElementById('tech_skill_group').innerHTML = response.array[2]+""+response.array[3];
document.getElementById('tech_address').innerHTML = response.array[4];
},
error: function(data,XMLHttpRequest,xhr, textStatus, errorThrown) {
console.log("Status: " + textStatus); console.log("Error: " + errorThrown);
var err = JSON.parse(xhr.responseText);
console.log(err.Message);
var json = $.parseJSON(data);
alert(json.error);
}
});
但是我收到错误“ VM299:1 Uncaught SyntaxError:JSON中的意外令牌u在JSON.parse()的位置0”
以下是我的返回json的代码
<?php
header("Content-Type: application/json");
include "../common/config.php";
include"../includes.php";
include"session.php";
// print_r('<script>console.log('.$_POST['id'].')</script>');
$technician_details=$db->get_a_line("select * from user where user_id='".$_POST['id']."'");
$about_me=$technician_details['about_me'];
$address=$technician_details['address'];
$assest_group=$technician_details['assest_group'];
$skill_group=$technician_details['skill_group'];
$skill_list=$technician_details['skill_list'];
$data = array();
$data[]=$about_me;
$data[]=$assest_group;
$data[]=$skill_group;
$data[]=$skill_list;
$data[]=$address;
$json_data = array(
"recordsTotal" => '5', // total number of records
"array" => $data // total data array
);
echo json_encode($json_data); // send data as json format
?>
它曾经在工作。但是在昨天的测试中,我注意到它给了我本地计算机以外的其他计算机错误。从明天早上开始,它开始在本地发送相同的错误。请帮忙。
答案 0 :(得分:1)
在您发表最后评论之后,我迅速进行了一个小型测试来模拟您的脚本在做什么。这样就可以了。
我建议先测试as-is
,然后如果您满意的话,请取消注释include
行,然后再次测试。然后,如果可行,请尝试使用实际的数据库查询等
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' ){
if( !empty( $_POST['id'] ) ){
/*
# are these being included correctly?
include '../common/config.php';
include '../includes.php';
include 'session.php';
*/
# to emulate db query and results
$id=filter_input( INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT );
$sql=sprintf( 'select * from user where user_id="%s"',$id );
$payload=array(
'about' => 'From db - all about user - based upon id '.$id,
'assetgroup' => 'From db - which asset group',
'skillgroup' => 'From db - which skill group',
'skilllist' => 'from db - what skills',
'address' => 'from db - address of tech'
);
$data = array(
'recordsTotal' => count( $payload ),
'data' => $payload,
'sql' => $sql
);
header( 'Content-Type: application/json' );
exit( json_encode( $data ) );
}
exit('error');
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>basic jquery - error parsing response</title>
<script src='//code.jquery.com/jquery-latest.js'></script>
<script>
document.addEventListener('DOMContentLoaded',()=>{
const node=function(id){
return document.getElementById( id )
};
let val=303;
$.ajax({
type:'POST',
url:location.href, //techtest.php
dataType:'json',
data:{ id: val },
success:function( r ){
node('tech_about_me').innerHTML = r.data.about;
node('tech_assest_group').innerHTML = r.data.assetgroup;
node('tech_skill_group').innerHTML = r.data.skillgroup+" "+r.data.skilllist;
node('tech_address').innerHTML = r.data.address;
console.info( r.sql, r.recordsTotal )
},
error: function(e) { alert(e); }
});
});
</script>
</head>
<body>
<div id='tech_about_me'></div>
<div id='tech_assest_group'></div>
<div id='tech_skill_group'></div>
<div id='tech_address'></div>
</body>
</html>