简体   繁体   English

AJAX 请求返回“未定义”

[英]AJAX Request returns 'undefined'

For some reason I always receive undefined if I return the value but if I'm trying to display it in alert I receive the php values.出于某种原因,如果我返回值,我总是会收到undefined ,但如果我试图在警报中显示它,我会收到 php 值。


function getXMLHttp() {
  var xmlHttp

  try {
    //Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  } catch(e) {
    //Internet Explorer
    try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        //Browser does not support AJAX
        return false;
      }
    }
  }

  return xmlHttp;
}

function isUsernameExists() {
  var xmlHttp = getXMLHttp();

  xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState == 4) {
    handleResponse(xmlHttp.responseText);
    }
  }

  var str = document.getElementById('username').value.toString();
  xmlHttp.open("GET", "ajax.php?username="+str, true);
  xmlHttp.send(null);

}

Edit:编辑:


function handleResponse(response) {
    return response.toString();
}

Thanks,谢谢,

Guy Dor盖伊多

It looks like you are trying to read the return value from this function:看起来您正在尝试从此 function 读取返回值:

function isUsernameExists() {

It doesn't have a return statement, so it will always be undefined .它没有return语句,所以它总是undefined

I'm guessing you expect this return statement to pass the value you want:我猜你希望这个 return 语句传递你想要的值:

return xmlHttp.responseText.toString();

But that is part of this function:但这是 function 的一部分:

xmlHttp.onreadystatechange = function() {

Which is called automatically when the readystatechange event fires, and not by any function call you make.当 readystatechange 事件触发时会自动调用,而不是由您调用的任何 function 调用。

Asynchronous JavaScript and XML rarely uses XML but is asynchronous.异步 JavaScript 和 XML 很少使用 XML,但它们是异步的。 Anything you want to do with the data fetched needs to be done by the callback function you assign to onreadystatechange .您想要对获取的数据执行的任何操作都需要通过您分配给 onreadystatechange 的回调 function 来完成 It can call other functions, but it cannot return anything (at least not that will be received anywhere useful).它可以调用其他函数,但它不能返回任何东西(至少不会在任何有用的地方收到)。

We can't see enough of your code to fully understand the situation, but you are dealing with an asynchronous operation and the only way to communicate the result of that asynch operation is to call a function with the value you got from that operation.我们看不到足够的代码来完全理解这种情况,但是您正在处理异步操作,并且传达该异步操作结果的唯一方法是使用您从该操作中获得的值调用 function。 You cannot just return from the function.您不能只从 function 返回。

This is a common misunderstanding (the sixth time I've answered this issue today).这是一个常见的误解(我今天第六次回答这个问题)。 The function on have for the ready state change is not called by any of your code. function 上的 state 更改不会被您的任何代码调用。 It's called by the browser internally when the ajax call completes.当 ajax 调用完成时,它由浏览器内部调用。 That means that returning your value from that function will do nothing and certainly won't communicate that result to any of your code.这意味着从 function 返回您的值不会做任何事情,当然不会将该结果传达给您的任何代码。 If you want to communicate that result to some of your code, then you need to call some of your code from inside that function and pass it the desired result so that code can act on it.如果您想将该结果传达给您的某些代码,那么您需要从 function 内部调用您的一些代码并将所需的结果传递给它,以便代码可以对其进行操作。 This breaks the normal flow of programming, but is required when dealing with asynchronous operations.这打破了正常的编程流程,但在处理异步操作时是必需的。

you have a return statement in the function your create here,您在此处创建的 function 中有一个 return 语句,

xmlHttp.onreadystatechange = function() {

but there is no return statement in the function,但是function中没有return语句,

function isUsernameExists() {

just because you created the function within another function does not mean the code in that function gets run.仅仅因为您在另一个 function 中创建了 function 并不意味着 function 中的代码可以运行。

the code within the function you created will not run until a response is received from the server, by which point the original function that created the second function will have already finished execution.在收到来自服务器的响应之前,您创建的 function 中的代码将不会运行,此时创建第二个 function 的原始 function 将已经完成执行。

the code does not halt to wait for a response from the server, it instead continues to run and finishes execution.代码不会停止等待来自服务器的响应,而是继续运行并完成执行。 the function that you created to handle the response from the server then gets run at a later time when a response is received.您为处理来自服务器的响应而创建的 function 然后在稍后收到响应时运行。

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

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