Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.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
Javascript 在获取内容之前检查文件是否存在_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript 在获取内容之前检查文件是否存在

Javascript 在获取内容之前检查文件是否存在,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我有一些简单的ajax代码,可以在服务器上获取文本文件的内容。 然后将内容放入文本区域。 如果文件不存在,它会在控制台中抛出404错误 所以我尝试使用php。 但是当我使用php时,没有返回任何内容 NoteFileName是先前创建的获取文件名的字符串(例如myfile.txt)。 addnote是textarea的id 给出404错误的原始代码(如果文件不存在): 我对php的尝试: $.ajax({ url: 'ajaxfilenotesget.php', type: 'G

我有一些简单的ajax代码,可以在服务器上获取文本文件的内容。 然后将内容放入文本区域。 如果文件不存在,它会在控制台中抛出404错误

所以我尝试使用php。 但是当我使用php时,没有返回任何内容

NoteFileName是先前创建的获取文件名的字符串(例如myfile.txt)。 addnote是textarea的id

给出404错误的原始代码(如果文件不存在):

我对php的尝试:

$.ajax({
    url: 'ajaxfilenotesget.php',
    type: 'GET',
    data: {
        NoteFileName: NoteFileName,
    },
    success: function() {
    document.getElementById("addnote").innerHTML = notedata;
    }
});
外部php文件:

  $filename = "upload/" . $_GET['NoteFileName'];

  if (file_exists($filename)) {
    $notedata = file_get_contents($filename);
  }
  else {
    $notedata = "";
  }
我也尝试过(以及其他的变化):

刚更改为此,在文本区域中未定义:

$.ajax({
    url: 'ajaxfilenotesget.php',
    type: 'GET',
    data: {
        NoteFileName: NoteFileName,
        success: function(data) {
    document.getElementById("addnote").innerHTML = data;
    }
    }
});

这是最终的解决方案,现在一切正常,没有错误

Ajax代码:

$.ajax({
    url: 'ajaxfilenotesget.php',
    type: 'GET',
    data: {NoteFileName: NoteFileName},
    success: function(notedata) {
      document.getElementById("addnote").value = notedata;
    }
});
外部PHP文件代码:

$filename = "upload/" . $_GET['NoteFileName'];

if (file_exists($filename)) {
  $notedata = file_get_contents($filename);
}
else {
  $notedata = "";
}

echo $notedata;

您是否
echo
$notedata
?控制台中出现了404错误,或者网络选项卡中出现了404响应代码?404仅在控制台中。除此之外,一切正常。echo,应该是我的原始代码还是php代码?存在和不存在时,console.log(NoteFileName)是什么
$.ajax({
    url: 'ajaxfilenotesget.php',
    type: 'GET',
    data: {NoteFileName: NoteFileName},
    success: function(notedata) {
      document.getElementById("addnote").value = notedata;
    }
});
$filename = "upload/" . $_GET['NoteFileName'];

if (file_exists($filename)) {
  $notedata = file_get_contents($filename);
}
else {
  $notedata = "";
}

echo $notedata;