首页 > 解决方案 > Read a text file with PHP and return it to JavaScript

问题描述

I want to get the content of a txt file in JavaScript, so I want to call a php file to open and save the content of the text file into a variable and return it to JS is that possible? I've sent variables from js to php with $.post() and I tried to use $.get("php file") but I can't return the file content can anybody help me?

标签: javascriptphp

解决方案


要从服务器读取/获取文件并在 javascript/jquery 中使用它的内容,您可以使用速记方法$.get。在回调函数内部,您可以处理从服务器返回的数据。

$.get("file.php", function(data) {
  console.log(data);
});

推荐阅读