如何检索收到的jquery ajax响应

时间:2017-05-04 09:07:24

标签: javascript php jquery ajax

Jquery代码:

     $("#sub").click(function(){
        var country = $('#sel-country :selected').text();
        var university = $('#sel-university :selected').text();
        var level = $('#sel-level :selected').text();
            $.ajax({
                url: 'getUsers.php',
                type: 'post',           
                data: { country:country,
                        university:university,
                        level:level
                      },           
                success:function(response1){
                },      
            });
        });
    });

PHP代码:

if(isset($_POST['university']) && isset($_POST['country']) ){

 $university = $_POST['university'];
 $country = $_POST['country'];
 $level = $_POST['level'];
 $sql="SELECT Distinct course FROM courses where 
 university='".$university."' and level ='".$level. "'"  ;
 $results=mysqli_query($dbhandle,$sql) or die("Cannot execute query");
 $count=mysqli_num_rows($results);
 $users_arr = array();
 while( $row = mysqli_fetch_array($results) ){
 $course = $row['course'];
 $users_arr[] = array("course" => $course);  
 }
  echo json_encode($users_arr);
 }

当我控制响应时,我得到以下输出

 [{"University":"Wuhan"},{"University":"CTGU"}][{"course":"MD"},{"course":"MS"}]

我没有指定数据类型,也没有为响应完成json解析 如果我这样做,我会收到像

这样的错误
t1.php:1 Uncaught SyntaxError: Unexpected token [ in JSON at position 46
at JSON.parse (<anonymous>)
at Object.success (t1.php:77)
at j (jquery-2.1.4.min.js:2)
at Object.fireWith [as resolveWith] (jquery-2.1.4.min.js:2)
at x (jquery-2.1.4.min.js:4)
at XMLHttpRequest.<anonymous> (jquery-2.1.4.min.js:4)

请帮助解决此问题......

0 个答案:

没有答案