使用 javascript 读取文件

发布于 2024-08-04 07:33:03 字数 35 浏览 2 评论 0原文

如何使用 JavaScript 从服务器端文件读取内容?

How do I read contents from a server side file using javascript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

温暖的光 2024-08-11 07:33:03

使用 Ajax 向 Web 服务器请求该信息。以 jQuery 为例:

jQuery.get('path/to/file/on/server.txt', null, function(data, status) {
    // your file contents are in 'data'
});

Ask the web server for it with Ajax. In jQuery speak, for instance:

jQuery.get('path/to/file/on/server.txt', null, function(data, status) {
    // your file contents are in 'data'
});
神回复 2024-08-11 07:33:03

使用 Ajax (XmlHttpRequest) 例如使用 jQuery:

jQuery.get( url, [data], [callback] ,[类型])

using Ajax (XmlHttpRequest) e.g. using jQuery:

jQuery.get( url, [data], [callback], [type] )

梦里兽 2024-08-11 07:33:03

您必须通过 HTTP 请求提供该文件(即,该文件可以作为 URL 提供,如 www.conphloso.com/somefile.txt),您可以通过 ajax 请求 在后台。

You have to serve the file via a HTTP request (i.e., the file is available as a URL like www.conphloso.com/somefile.txt), which you can grab via an ajax request in the background.

故事和酒 2024-08-11 07:33:03

使用纯 JavaScript 是不可能实现这一点的。 Javascript 在客户端浏览器中运行,您无法访问服务器中的文件。您可以使用 AJAX 来执行此操作。

This is not possible using plain javascript. Javascript runs in the client browser and you cannot access a file in server. You can use AJAX to do this.

久光 2024-08-11 07:33:03

快速回答是“你不能”。

如果您使服务器端文件可以通过 Web 服务器访问,则可以使用 xmlhttprequest(又名 ajax)来检索它。

The quick answer is "you can't".

If you make the server side file accessible through your web server, you can use an xmlhttprequest, a.k.a ajax, to retrieve it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文