我正在尝试使用 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: Unexpected token u in JSON at position 0 at JSON.parse ()”
以下是我返回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
?>
它以前工作过。但是在昨天测试时,我注意到它在我本地以外的 PC 上出错。从明天早上开始,它开始在本地出现同样的错误。请帮忙。
在您发表最后一条评论后,我迅速组织了一个小测试来模拟您的脚本正在做什么。这工作正常。
我建议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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句