【问题标题】:error in performing ajax post parameters执行ajax post参数时出错
【发布时间】:2017-08-23 04:12:16
【问题描述】:

我被要求执行 ajax 发布请求以从最后一行获取 student_name 和 student_gender。我还想从数据库中获取全部数据。最后一行的学生姓名和性别分别为“yyyyty”和“F”。我希望将整个数据和提取的数据(学生姓名和性别)分别放在控制台页面中。当我对声明的 php 脚本执行 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 发布参数从最后一行获取学生姓名和性别,并从整个数据中单独显示这些数据。我想回显 2 次,这些数据显示了整个数据默认数据并显示提取的数据...可以吗

【问题讨论】:

  • 您的 json 无效(从 js 方面看)因为在 php 方面您有两个回显:第一次是来自数据库查询的数组,另一个是在您的“else”子句中。
  • 但我希望默认显示所有数据.. 然后当我执行 ajax 发布请求时,我希望显示提取的数据
  • 无论你想要什么,你都会得到你发送的:无效的 json。找到一种正确编码你的东西的方法,以便前端可以获取数据并执行它的操作。

标签: php jquery json ajax


【解决方案1】:

您正在从服务器发送无效的 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 抱歉这是我第一次使用这个论坛......我没有阅读这个论坛的规则............我应该发布更新的问题在同一个问题中还是我应该创建另一个问题
  • @Tariq :更喜欢新问题而不是更新已回答的问题。另外,请正确阅读此答案:您没有实现 ruhul (正确)建议的内容。
  • @YvesLeBorg 我知道...但我想默认显示所有数据然后显示 ajax 发布请求数据
猜你喜欢
  • 2013-05-31
  • 1970-01-01
  • 2019-04-19
  • 1970-01-01
  • 2011-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多