Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 执行ajax post参数时出错_Php_Jquery_Json_Ajax - Fatal编程技术网

Php 执行ajax post参数时出错

Php 执行ajax post参数时出错,php,jquery,json,ajax,Php,Jquery,Json,Ajax,我被要求执行ajax post请求,从最后一行获取学生姓名和性别。。我还想从数据库中获取全部数据。最后一行的学生姓名和性别分别为“yyyyty”和“F”。我希望将整个数据和提取的数据(学生姓名和性别)分别放在控制台页面中 Status Code: 200 ErrorThrown: SyntaxError: Unexpected token { in JSON at position 1634 jqXHR.responseText: [{"student_id":"1","studen

我被要求执行ajax post请求,从最后一行获取学生姓名和性别。。我还想从数据库中获取全部数据。最后一行的学生姓名和性别分别为“yyyyty”和“F”。我希望将整个数据和提取的数据(学生姓名和性别)分别放在控制台页面中

   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>


您正在从服务器发送无效的json响应

echo json_encode($json_array[count($json_array)-1]['student_name']);
echo json_encode($json_array[count($json_array)-1]['student_gender']);
echo json_encode($json_array);
这将一起生成一个无效的json字符串。不要回显它们,而是先将它们存储在一个数组中,然后回显整个数组

$response = [];
$response['student_name'] = $json_array[count($json_array)-1]['student_name'];
$response['student_gender'] = $json_array[count($json_array)-1]['student_gender'];
$response['whole_result'] = $json_array;

echo json_encode($response);

希望有帮助。

这个
$response={}
应该是
$response=[]
@vher2,@ruhul非常感谢您!!!!!我将更新我的问题,因为我更新的代码出现问题@YvesLeBorg抱歉,这是我第一次使用此论坛。。。。。我没有阅读本论坛的规则。。。。。。。。。。。我应该在同一个问题中发布更新后的问题,还是创建另一个问题questions@Tariq:与更新已回答的问题相比,更喜欢新问题。另外,请正确阅读此答案:您没有实施ruhul(正确)提出的建议。@YvesLeBorg我知道。。。但是我想在默认情况下显示所有数据,然后显示ajax post请求数据您的json无效(从js端可以看到),因为在php端有两个echo:第一个是来自DB查询的数组,另一个是在'else'子句中。但是我想在默认情况下显示所有数据。。然后,当我执行ajax post请求时,我希望显示提取的数据。不管您想要什么,您都会得到您发送的内容:无效的json。找到一种正确编码你的东西的方法,这样前端就可以得到数据并完成它的工作。
$response = [];
$response['student_name'] = $json_array[count($json_array)-1]['student_name'];
$response['student_gender'] = $json_array[count($json_array)-1]['student_gender'];
$response['whole_result'] = $json_array;

echo json_encode($response);