在纯JavaScript中将文本附加到服务器上的文件? [英] Append text to file on server in pure javascript?

查看:59
本文介绍了在纯JavaScript中将文本附加到服务器上的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为标题。

我正在维护一个网页系统,并在其上添加一些新功能。我想将日志写入服务器上的文件而不是console.log(),因为我想知道发生了什么以及何时发生,我认为这可能会让我更容易追踪错误。但我在Google上搜索了几个解决方案,一些使用ActiveXObject,一些使用Node.js.而且我没有在Edge上使用ActiveXObject。

我怎么能成功?



我尝试了什么:



As title.
I am maintaining a web page system, and adding some new features on it. I want to write log to a file on server instead of console.log() because I want to know what happened and when, and I think that it may make me tracing bugs easier. But I searched on Google for several solutions, some uses ActiveXObject and some uses Node.js. And I failed to use ActiveXObject on Edge.
How could I make it?

What I have tried:

function WriteLog(eventdata){
    var fs=new ActiveXObject("Scripting.FileSystemObject");
    var f=fs.OpenTextFile("D:\\log.log",2,true);
    f.Write(eventdata+"\r\n");
}



我在Edge上执行了这个函数并得到一个错误,上面写着'ActiveXObject'未定义。


I executed this function on Edge and get an error says "'ActiveXObject' is not defined".

推荐答案

您无法从浏览器中运行的代码写入服务器上的文件。



ActiveX不是由任何当前浏览器支持,因为它被发现是不安全的。



您可以对Web服务器公开的方法使用ajax调用来告诉它写入内容登录。以示例中的javascript ajax call为目标。您还必须编写一个Web Api方法来为浏览器调用一些东西。
You're not going to be able to write to a file on the server from code running in the browser.

ActiveX isn't supported by any current browsers because it was found to be insecure.

You can use an ajax call to a method the web server exposes to tell it to write something to the log. Goggle for "javascript ajax call" for examples. You're also going to have to write a Web Api method to give the browser something to call.


这篇关于在纯JavaScript中将文本附加到服务器上的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆