Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.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 responseText作为字符串返回_Ajax_Responsetext - Fatal编程技术网

将AJAX responseText作为字符串返回

将AJAX responseText作为字符串返回,ajax,responsetext,Ajax,Responsetext,在我的代码中,我尝试使用AJAX获取模板文件的内容,并将其作为字符串返回,我对此发出警告。这会导致未定义,但是如果不是返回xmlhttp.responseText而是警报(xmlhttp.responseText)我得到了我想要的结果 我可以在函数外以字符串形式返回AJAX内容吗 警报(getFile()); 函数getFile(){ var xmlhttp=new XMLHttpRequest(); xmlhttp.onreadystatechange=函数stateChanged(){ i

在我的代码中,我尝试使用AJAX获取模板文件的内容,并将其作为字符串返回,我对此发出警告。这会导致
未定义
,但是如果不是
返回xmlhttp.responseText
而是
警报(xmlhttp.responseText)
我得到了我想要的结果

我可以在函数外以字符串形式返回AJAX内容吗

警报(getFile());
函数getFile(){
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=函数stateChanged(){
if(xmlhttp.readyState==4)
返回xmlhttp.responseText;
}
open(“POST”,“/template.html”,true);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
xmlhttp.send();

}
否:要执行所需操作,您需要在
getFile
函数中接受回调并调用它:

function getFile(callback) {

  var xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange = function stateChanged() {
    if (xmlhttp.readyState == 4)
      callback(xmlhttp.responseText);
  }

  xmlhttp.open("POST","/template.html", true);
  xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlhttp.send();
}

getFile(alert);

否:要执行所需操作,需要在
getFile
函数中接受回调并调用它:

function getFile(callback) {

  var xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange = function stateChanged() {
    if (xmlhttp.readyState == 4)
      callback(xmlhttp.responseText);
  }

  xmlhttp.open("POST","/template.html", true);
  xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlhttp.send();
}

getFile(alert);

XMLHttpRequest是异步的。您应该像下面那样使用回调函数

getFile(alert);

function getFile(callback) {

  var xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange = function stateChanged() {
    if (xmlhttp.readyState == 4)
      callback(xmlhttp.responseText);
  }

  xmlhttp.open("POST","/template.html", true);
  xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlhttp.send();
}

XMLHttpRequest是异步的。您应该像下面那样使用回调函数

getFile(alert);

function getFile(callback) {

  var xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange = function stateChanged() {
    if (xmlhttp.readyState == 4)
      callback(xmlhttp.responseText);
  }

  xmlhttp.open("POST","/template.html", true);
  xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlhttp.send();
}

很酷,因此,与其提醒结果,不如将其作为字符串返回,以便在其他地方使用?(另外,如果我想将
“/template.html”
作为参数传递给函数,我该怎么做?您只能在回调中使用result。签名是
getFile(fileName,callback)
像Node.js的函数
fs.readFile(file,callback)
好的,所以我根本无法从函数中提取值?很酷,所以我不需要提醒结果,如何将其作为字符串返回,我可以在其他地方使用?(另外,如果我想将
“/template.html”
作为参数传递给函数,我该怎么做?只能在回调中使用result。签名是
getFile(fileName,callback)
像Node.js的函数
fs.readFile(file,callback)
Ok,所以我根本无法从函数中提取值?可能重复的可能重复的