错误:XMLHttpRequest.ajax.onreadystatechange上JSON.parse(<anonymous>)位置0处的JSON中的意外令牌<

时间:2019-02-12 21:52:21

标签: javascript php

我使用Javascript和Ajax从服务器发送和接收数据。我读了一些服务器文件中的错误消息,为什么它给出了这个错误但我找不到任何错误。

我的html文件     

<select class="form-control" id="semester" name="semester">
    <option value=-1>Select Semester</option>
    <option value=1>Semester </option>
    <option value=1>Semester 1</option>
    <option value=2>Semester 2</option>
    <option value=3>Semester 3</option>
    <option value=4>Semester 4</option>
    <option value=5>Semester 5</option>
    <option value=6>Semester 6</option>
    <option value=7>Semester 7</option>
    <option value=8>Semester 8</option>
</select>

<<select class="form-control" id="category_id" name="category_id">
    <option value=-1>Select Catagory</option>
    <option value=1>Institutional Reqt.</option>
    <option value=2>Adv. Core</option>
    <option value=3>Foundation</option>
    <option value=4>Adv. Elect</option>
</select>

<input class="btn btn-primary" type='button' id='find' value='find' name="find">

</form>

<table>
    <tr>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Job Title</th>
    </tr>

    <tbody id="data">

    </tbody>
</table>

我的JavaScript文件

这是发送的学期号和用户想要查看的类别。

  <script>
        $("#find").click(function(){
        var semester=$("#semester").val();
        var category_id=$("#category_id").val();
        $.ajax({
                url:'data.php',
                type:'GET',
                data:{semester:semester,category_id:category_id},
                success:function(data){
                    console.log(data);
                    if(data==1){
                        console.log("Very noice");
                    }
                }
            })  
        });

这是从服务器接收信息。
        //调用ajax

    var ajax= new XMLHttpRequest();
    var method='POST';
    var url = 'data.php';
    var asynchronous = true;

    ajax.open(method, url, asynchronous);
// sending ajax request
    ajax.send();    

//receiving response from data.php
    ajax.onreadystatechange = function(){

        if(this.readyState == 4 && this.status == 200)
        {
            //converting JSON back to array
            var data = JSON.parse(this.responseText);
            console.log(data); 

            var html = "";
            //loopinh through the data
            for(var a=0;a<data.length;a++){
                var firstName=data[a].semester;
                var lastName=data[a].course_title;
                var jobTitle = data[a].course_desc;

                html += "<tr>";
                html += "<td>" + firstName + "</td>";
                html += "<td>" + lastName + "</td>";
                html += "<td>" + jobTitle + "</td>";
                html += "</tr>";

                document.getElementById("data").innerHTML = html; 

            }
        }
    }

</script>

这是我的php文件

<?php

//getting data from database

$conn = mysqli_connect("localhost","root","","bscis1620");


$semester = $_GET['semester'];
$category_id= $_GET['category_id'];



    $query = 'select * from bscis1620';
    $check = 0;
    $semesterq="$semester = bscis1620.semester";
    $category_idq="$category_id = bscis1620.category_id";
    if($semester>0){
        if($check == 0){
            $query = $query. " where ". $semesterq;
            $check++;
        }
        else{
            $query = $query. " and ". $semesterq;
        }
    }
    if($category_id>0){
        if($check == 0)
            $query = $query. " where ". $category_idq;
        else{
            $query = $query. " and ". $category_idq;
        }
    }


    $result=mysqli_query($conn, $query);



$data = array();
while($row = mysqli_fetch_assoc($result))
{
    $data[] = $row;
}

echo json_encode($data);

?>

我已经通过console.log检查了我的查询,并且从数据库中获得了正确的信息,我将非常感谢您的帮助。

0 个答案:

没有答案