简体   繁体   English

AJAX请求返回为“未定义”

[英]AJAX request returns as 'undefined'

I am trying to perform a simple AJAX request from a table but when I var dump the result comes back as undefined? 我试图从表中执行一个简单的AJAX请求,但是当我进行var dump时,结果返回未定义状态?

Here is the JS: 这是JS:

function showUser(str) {
  if (str=="") {
    document.getElementById("txtHint").innerHTML="";
    return;
  } 
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else { // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (this.readyState==4 && this.status==200) {
      document.getElementById("txtHint").innerHTML=this.responseText;
    }
  }
  xmlhttp.open("GET","actions/teamData.php?q="+str,true);
  xmlhttp.send();
}

I have created a form that select strictly against ID 1 because when trying to do this dynamically with Php it was doing the same so I have set it to 1 to be sure for now: 我创建了一个严格按照ID 1进行选择的表单,因为当尝试使用Php动态地执行此操作时,它也是如此,因此我现在将其设置为1:

<option value="1">' . $row['name'] . '</option>

this is the php doing the request: 这是做请求的PHP:

$q = intval($_GET['q']);

var_dump($_GET);

$sql = "SELECT * FROM team WHERE id = ' " . $q . " ' ";
$result = mysqli_query($conn,$sql);

echo "<table>
<tr>
<th>Team name</th>
</tr>";

while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['name'] . "</td>";
    echo "</tr>";
}
echo "</table>";

mysqli_close($conn);

I have a valid connection which has been tested. 我有一个经过测试的有效连接。 But the result comes back as: 但是结果返回为:

array(1) { ["q"]=> string(9) "undefined" }
array(1) { ["q"]=> string(9) "undefined" }

You have got it because javascript variable "str" is undefined. 之所以得到它,是因为javascript变量“ str”未定义。 Just check it and send a valid ID. 只需检查并发送有效的ID。

Start writing you function in a next way: 开始以另一种方式编写函数:

function send_xhr(str) {
  str = str || '';
  if ( !str ) { // str is empty
    // Do something
  }

  // str is not empty
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM