我被要求对php脚本执行ajax post请求,以获取最后一行中的一些数据(学生姓名和学生性别,分别是yyyyty和F)。默认情况下,我想在执行ajax帖子时显示所有数据。然后,我想使用ajax post请求回显所选数据。我的代码在下面找到............ 当我执行ajax帖子时,我收到了错误信息
Status Code: 200
ErrorThrown: SyntaxError: Unexpected token { in JSON at position 1634
jqXHR.responseText:
[{"student_id":"1","student_name":"Ashfur","student_gender":"F","student_age":"19","student_religion":"Muslim","student_course_id":"1"},{"student_id":"2","student_name":"Irfan","student_gender":"M","student_age":"17","student_religion":"Islam","student_course_id":"4"},{"student_id":"3","student_name":"Alice","student_gender":"F","student_age":"21","student_religion":"Chinese","student_course_id":"2"},{"student_id":"4","student_name":"Mohit","student_gender":"M","student_age":"20","student_religion":"Christian","student_course_id":"6"},{"student_id":"5","student_name":"Susy","student_gender":"F","student_age":"27","student_religion":"Chirstian","student_course_id":"5"},{"student_id":"6","student_name":"Ida","student_gender":"F","student_age":"23","student_religion":"Islam","student_course_id":"3"},{"student_id":"7","student_name":"Abdul","student_gender":"M","student_age":"22","student_religion":"Islam","student_course_id":"1"},{"student_id":"8","student_name":"Ernest","student_gender":"M","student_age":"25","student_religion":"Chinese","student_course_id":"4"},{"student_id":"9","student_name":"Wei Ling","student_gender":"F","student_age":"23","student_religion":"Chinese","student_course_id":"2"},{"student_id":"10","student_name":"Ashtae","student_gender":"M","student_age":"23","student_religion":"Islam","student_course_id":"4"},{"student_id":"11","student_name":"Jasmine","student_gender":"F","student_age":"23","student_religion":"Chinese","student_course_id":"2"},{"student_id":"65656","student_name":"yyyyty","student_gender":"F","student_age":"65","student_religion":"anything","student_course_id":"009090"}]{"student_name":"yyyyty","student_gender":"F"}
我的html文件
<html>
<head>
<script type="text/javascript" src="/Cesium-1.34/ThirdParty/jquery-1.11.3.min.js"></script>
</head>
<div id="resulte"</div>
<script type="text/javascript">
showData();
function showData()
{
$.ajax({
type: "post",
url: "student.php",
dataType: "json",
data: {
lastOnly: true,
},
success: function(data){
console.log(data);
},
error: function(jqXHR, textStatus, errorThrown) {
alert('An error occurred... Look at the console (F12 or Ctrl+Shift+I, Console tab) for more information!');
$('#resulte').html('<p>Status Code: '+jqXHR.status+'</p><p>ErrorThrown: ' + errorThrown + '</p><p>jqXHR.responseText:</p><div>'+jqXHR.responseText + '</div>');
console.log('jqXHR:');
console.log(jqXHR);
console.log('textStatus:');
console.log(textStatus);
console.log('errorThrown:');
console.log(errorThrown);
},
});
};
</script>
</body>
</html>
在php脚本中
<?php
$conn = mysqli_connect('localhost','root','netwitness') or die ("Could not connect database");
$db = mysqli_select_db($conn,'abdpractice') or die ('Could not select database');
$result = mysqli_query($conn,"select * from student");
$json_array = array();
while ($row = mysqli_fetch_assoc($result))
{
$json_array[] = $row;
}
echo json_encode($json_array);
if (!isset($_POST["lastOnly"])){
} else {
$response = [];
$response['student_name'] = $json_array[count($json_array)-1]['student_name'];
$response['student_gender'] = $json_array[count($json_array)-1]['student_gender'];
echo json_encode($response);
}
?>
我想默认回显整个数据。当我执行ajax post请求时,我想再次回显所选数据...请帮助我。
谢谢。
答案 0 :(得分:2)
错误告诉你会发生什么。
您的JSON无效:
[{"student_id":"1","student_name":"Ashfur","student_gender":"F","student_age":"19","student_religion":"Muslim","student_course_id":"1"},{"student_id":"2","student_name":"Irfan","student_gender":"M","student_age":"17","student_religion":"Islam","student_course_id":"4"},{"student_id":"3","student_name":"Alice","student_gender":"F","student_age":"21","student_religion":"Chinese","student_course_id":"2"},{"student_id":"4","student_name":"Mohit","student_gender":"M","student_age":"20","student_religion":"Christian","student_course_id":"6"},{"student_id":"5","student_name":"Susy","student_gender":"F","student_age":"27","student_religion":"Chirstian","student_course_id":"5"},{"student_id":"6","student_name":"Ida","student_gender":"F","student_age":"23","student_religion":"Islam","student_course_id":"3"},{"student_id":"7","student_name":"Abdul","student_gender":"M","student_age":"22","student_religion":"Islam","student_course_id":"1"},{"student_id":"8","student_name":"Ernest","student_gender":"M","student_age":"25","student_religion":"Chinese","student_course_id":"4"},{"student_id":"9","student_name":"Wei Ling","student_gender":"F","student_age":"23","student_religion":"Chinese","student_course_id":"2"},{"student_id":"10","student_name":"Ashtae","student_gender":"M","student_age":"23","student_religion":"Islam","student_course_id":"4"},{"student_id":"11","student_name":"Jasmine","student_gender":"F","student_age":"23","student_religion":"Chinese","student_course_id":"2"},{"student_id":"65656","student_name":"yyyyty","student_gender":"F","student_age":"65","student_religion":"anything","student_course_id":"009090"}]{"student_name":"yyyyty","student_gender":"F"}
JSON的最后一部分无效:{"student_name":"yyyyty","student_gender":"F"}
您可以使用https://jsonformatter.curiousconcept.com/之类的工具来查看您的JSON是否有效。
正如Lawrence Cherone所说,你可以使用json_encode。
$myFinalJSON = json_encode($myFirstJSON);
答案 1 :(得分:0)
如果您希望在单个json响应中返回结果,则必须将两个数组合并
while ($row = mysqli_fetch_assoc($result))
{
$json_array[] = $row;
}
//echo json_encode($json_array);
if (!isset($_POST["lastOnly"])){
} else {
$response = [
'student_name' = $json_array[count($json_array)-1]['student_name'],
'student_gender' = $json_array[count($json_array)-1]['student_gender']
];
}
$json_array[] = $response;
echo json_encode($json_array);
答案 2 :(得分:0)
在窗口加载时发送AJAX请求,以便显示所有记录 当您触发按钮以获取最后一个数据时,您将从记录中获取最后一个数据。
注意:通常为了获取特定记录,我们发送一个搜索键,用于指定WHERE子句条件,并根据该搜索键执行我们的Sql / Mysql查询。
在这里,我没有发送任何搜索关键字,并且每次都不会获取所有记录(导致性能问题)。尝试避免这种情况,但只是为了澄清,在你的情况下,我只是使用一个按钮来触发if-else条件并且每次都获取所有记录只是为了根据条件用php演示ajax请求。
以下是代码
HTML文件
<form method='post'>
<button id='last'>Fetch Last</button>
</form>
<div id="result"></div>
JS档案
$(window).load(function() {
let val = 'all';
showData(val);
});
$('#last').on('click', function() {
let val = $(this).prop('id');
showData(val);
});
function showData(data)
{
$.ajax({
type: "post",
url: "student.php",
dataType: "json",
data: {
lastOnly: data,
},
success: function(data){
console.log(data);
},
error: function(jqXHR, textStatus, errorThrown) {
alert('An error occurred... Look at the console (F12 or Ctrl+Shift+I, Console tab) for more information!');
$('#resulte').html('<p>Status Code: '+jqXHR.status+'</p><p>ErrorThrown: ' + errorThrown + '</p><p>jqXHR.responseText:</p><div>'+jqXHR.responseText + '</div>');
console.log('jqXHR:');
console.log(jqXHR);
console.log('textStatus:');
console.log(textStatus);
console.log('errorThrown:');
console.log(errorThrown);
},
});
};
PHP文件
$conn = mysqli_connect('localhost','root','netwitness') or die ("Could not connect database");
$db = mysqli_select_db($conn,'abdpractice') or die ('Could not select database');
$result = mysqli_query($conn,"select * from student");
$response = array();
while ($row = mysqli_fetch_assoc($result))
{
$response[] = $row;
}
if (isset($_POST["lastOnly"]) && $_POST["lastOnly"] === 'last'){
$size = count($json_array);
$response['student_name'] = $json_array[$size-1]['student_name'];
$response['student_gender'] = $json_array[$size-1]['student_gender'];
echo json_encode($response);
} else {
echo json_encode($response);
}
还有一点是注意,即 -
使用.done()
和.fail()
方法来处理ajax响应,而不是使用success
和error
。
希望你能从这个解释中得到一些帮助。 :):)