Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.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
AJAX PHP服务器端响应,但我无法在客户端页面中显示响应_Php_Jquery_Ajax - Fatal编程技术网

AJAX PHP服务器端响应,但我无法在客户端页面中显示响应

AJAX PHP服务器端响应,但我无法在客户端页面中显示响应,php,jquery,ajax,Php,Jquery,Ajax,我有一个页面将一些输入数据发送到php页面,后者应重新发送响应,稍后来自服务器端的一些数据应显示在客户端页面上 我跟踪了请求,响应如下6[{“学生Id”:“21”,“学生姓名”:“Smith”}] 但我无法在客户端页面中显示结果 HTML脚本 <input type="text" name="Roomnum" id="Roomnum"> </input> <select id="Studen

我有一个页面将一些输入数据发送到php页面,后者应重新发送响应,稍后来自服务器端的一些数据应显示在客户端页面上

我跟踪了请求,响应如下6[{“学生Id”:“21”,“学生姓名”:“Smith”}]

但我无法在客户端页面中显示结果

HTML脚本

<input type="text" name="Roomnum" id="Roomnum">  </input>  
<select id="StudentsSelection" name="Student_Id" style="width:200px;height: 40px;"> 
  <option value="0">- Select -</option>
</select>

-挑选-
Ajax脚本

 var Room_Id = $("#Roomnum").val();
 $.ajax({
 url: 'getStudents.php',
 type: 'post',
 data: {Room_Id:Room_Id},
 dataType: 'json',
 success:function(response){
 
 console.log("success");
 console.log(response);
 var len = response.length;
  $("#StudentsSelection").empty();
  for( var i = 0; i<len; i++){
 var Student_Id = response[i]['Student_Id'];
 var Student_Name = response[i]['Student_Name'];
 $("#Roomnum").empty();
  $("#Roomnum").append(Student_Name);
  $('#StudentsSelection').append(`<option value="${Student_Id}"> 
  ${Student_Id}-${Student_Name}  
 </option>`); 
 }
    }  
        }); 
var Room_Id=$(“#Roomnum”).val();
$.ajax({
url:'getStudents.php',
键入:“post”,
数据:{Room_Id:Room_Id},
数据类型:“json”,
成功:功能(响应){
控制台日志(“成功”);
控制台日志(响应);
var len=响应长度;
$(“#学生选择”).empty();

对于(var i=0;i在将Ajax响应像JSON一样使用之前,请尝试将其解析为JSON,我已经尝试了您的代码,它只是一个字符串:

var Room_Id = $("#Roomnum").val();
$.ajax({
    url: 'getStudents.php',
    type: 'post',
    data: {Room_Id:Room_Id},
    dataType: 'json',
    success:function(response){
        response = JSON.parse(response);
        console.log("success");
        console.log(response);
        var len = response.length;
        $("#StudentsSelection").empty();
        for( var i = 0; i<len; i++){
            var Student_Id = response[i]['Student_Id'];
            var Student_Name = response[i]['Student_Name'];
            $("#Roomnum").empty();
            $("#Roomnum").append(Student_Name);
            $('#StudentsSelection').append(`<option value="${Student_Id}">
              ${Student_Id}-${Student_Name}
             </option>`);
        }
    }
});
var Room_Id=$(“#Roomnum”).val();
$.ajax({
url:'getStudents.php',
键入:“post”,
数据:{Room_Id:Room_Id},
数据类型:“json”,
成功:功能(响应){
response=JSON.parse(response);
控制台日志(“成功”);
控制台日志(响应);
var len=响应长度;
$(“#学生选择”).empty();

对于(var i=0;i在将Ajax响应像JSON一样使用之前,请尝试将其解析为JSON,我已经尝试了您的代码,它只是一个字符串:

var Room_Id = $("#Roomnum").val();
$.ajax({
    url: 'getStudents.php',
    type: 'post',
    data: {Room_Id:Room_Id},
    dataType: 'json',
    success:function(response){
        response = JSON.parse(response);
        console.log("success");
        console.log(response);
        var len = response.length;
        $("#StudentsSelection").empty();
        for( var i = 0; i<len; i++){
            var Student_Id = response[i]['Student_Id'];
            var Student_Name = response[i]['Student_Name'];
            $("#Roomnum").empty();
            $("#Roomnum").append(Student_Name);
            $('#StudentsSelection').append(`<option value="${Student_Id}">
              ${Student_Id}-${Student_Name}
             </option>`);
        }
    }
});
var Room_Id=$(“#Roomnum”).val();
$.ajax({
url:'getStudents.php',
键入:“post”,
数据:{Room_Id:Room_Id},
数据类型:“json”,
成功:功能(响应){
response=JSON.parse(response);
控制台日志(“成功”);
控制台日志(响应);
var len=响应长度;
$(“#学生选择”).empty();

对于(var i=0;i
$(“#StudentsSelection”).append(`Hi.what
$(“#Roomnum”).append(Student#Name);
您在输入框中追加值了吗?这不应该是
.val(Student#Name)
@Swati.val(Student#Name)也不起作用了吗?检查您的浏览器控制台是否有任何错误?@Swati$(“#StudentsSelection”).append(`Hi.what
$(“#Roomnum”).append(Student\u Name);
您是在输入框中追加值吗?不应该是
.val(Student\u Name)
@Swati.val(Student\u Name)也不起作用嗨,检查你的浏览器控制台是否有任何错误?@Swati现在解决了它谢谢你抱歉你的代码没有解决问题Roomnum输入没关系,$(“#Roomnum”).empty();$(“#Roomnum”).append(学生名称);仅检查响应抱歉,您的代码无法解决问题Roomnum输入不重要,$(“#Roomnum”).empty();$(“#Roomnum”).append(学生名称);仅检查响应