简体   繁体   English

使用javascript读取和写入外部文件?

[英]Reading and writing to an external file using javascript?

I have an online storage account that I`m using for my homepage. 我有一个用于主页的在线存储帐户。 Basically I have just made an "index.html" and stored there . 基本上,我刚刚制作了一个“ index.html”并存储在此处。 and no php , asp is possible . 没有php,asp是可能的。

So If I must create a message form on the homepage and store the message in a separate text file in JSON format ,can it be done using javascript ? 因此,如果必须在主页上创建消息表单并将消息存储在JSON格式的单独文本文件中,可以使用javascript做到吗? also I need to query the Text file whenver I want to display the messages using javascript . 每当我想使用javascript显示消息时,我也需要查询文本文件。

So far , I tried TaffyDB but realised it doesn`t have a way to persist the data after session closes. 到目前为止,我尝试了TaffyDB,但意识到在会话关闭后它还没有办法保留数据。 or maybe I missed something? 还是我错过了什么? Thanks! 谢谢!

Short answer. 简短的回答。 No. 没有。

The JavaScript is client side. JavaScript是客户端。 So it can do all sorts of cool stuff on the persons computer that visits your site but unless you're running some server side code that takes the JSON encoded data and does something with it then you're out of luck. 因此,它可以在访问您的网站的人机上做各种很酷的事情,但是除非您正在运行一些服务器端代码,这些代码接受JSON编码的数据并对其进行处理,否则您就不走运了。

There are many alternatives. 有很多选择。

If you don't want to run your own server side code then you could use a separate service like Parse.com that does REST and has a comprehensive API. 如果您不想运行自己的服务器端代码,则可以使用Parse.com之类的单独服务,该服务可以执行REST并且具有完善的API。

  • A mobile website can access Parse data from Javascript. 一个移动网站可以从Javascript访问解析数据。
  • A webserver can show data from Parse on a website. Web服务器可以在网站上显示来自Parse的数据。
  • You can upload large amounts of data that will later be consumed in a mobile app. 您可以上传大量数据,以后将在移动应用程序中使用这些数据。
  • You can download recent data to run your own custom analytics. 您可以下载最新数据以运行自己的自定义分析。
  • Applications written in any programming language can interact with data on Parse. 用任何编程语言编写的应用程序都可以与Parse上的数据进行交互。
  • You can export all of your data if you no longer want to use Parse. 如果您不再想使用Parse,则可以导出所有数据。

You can try with jQuery/AJAX. 您可以尝试使用jQuery / AJAX。 To read: 读书:

$.get("path_to_file", null, function(fileData) {
  alert(fileData);
  /* Your code goes here */
}, "text");

But in order to write, I think the only way is with some server-side language (PHP, ASP, etc) 但是为了编写,我认为唯一的方法是使用某些服务器端语言(PHP,ASP等)

The short answer is no. 最简洁的答案是不。

You need to have some server-side support to persist the data on that server. 您需要一些服务器端支持才能将数据持久存储在该服务器上。 You can, however, use client-side javascript to relay the information to a server that DOES support reading and writing of the data of course. 但是,您可以使用客户端javascript将信息中继到确实支持读取和写入数据的服务器。

Technically, node.js is javascript that does support file reading and writing - but I assume that's out of the question for your environment :) 从技术上讲,node.js是支持文件读写的javascript-但我认为这对您的环境是不可能的:)

One crazy way (just as a thought experiment) to implement persistent storage for your web application without server side support is to have the clients talk to each other through P2P. 在没有服务器端支持的情况下为Web应用程序实现持久存储的一种疯狂方法(仅作为思想实验)是让客户端通过P2P相互通信。 This is possible with Flash or some java applet..etc. 这可以通过Flash或某些Java applet..etc实现。 So as long as one client is up (perhaps your own comupter!), you'll have some form of persistent storage. 因此,只要有一个客户端启动(也许是您自己的计算机!),您就会拥有某种形式的持久性存储。 Your server/webpage simply serves up this embedded object which does the actual work. 您的服务器/网页只需提供该嵌入式对象即可完成实际工作。

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

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