将 javascript 输出写入服务器上的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11240996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write javascript output to file on server
提问by alpinemobile
So I have this HTML file that tests the user's screen resolution, and plugins installed using Javascript. So when the user accesses the page it sees: (e.g.) Your current screen resolution is 1024x768 and you have the following plugins installed: Plug-in No.2- Java Deployment Toolkit 7.0.10.8 [Location: npdeployJava1.dll], Plug-in No.3- Java(TM) Platform SE 7 U1 [Location: npjp2.dll], Plug-in No.4- Microsoft Office 2003 [Location: NPOFFICE.DLL]... I also need to save this information in a file on the server. All users are having firefox or chrome. How do I do this using AJAX?
所以我有这个测试用户屏幕分辨率的 HTML 文件,以及使用 Javascript 安装的插件。因此,当用户访问它看到的页面时:(例如)您当前的屏幕分辨率是 1024x768 并且您安装了以下插件:插件 No.2-Java 部署工具包 7.0.10.8 [位置:npdeployJava1.dll],插件-在 No.3-Java(TM) Platform SE 7 U1 [位置:npjp2.dll],插件 No.4-Microsoft Office 2003 [位置:NPOFFICE.DLL]...我还需要将此信息保存在服务器上的文件。所有用户都使用 Firefox 或 chrome。如何使用 AJAX 执行此操作?
<html>
<body>
<script language="JavaScript1.2">
document.write("Your current resolution is "+screen.width+"*"+screen.height+"")
</script>
<BR><BR>
<SCRIPT LANGUAGE="JavaScript">
var num_of_plugins = navigator.plugins.length;
for (var i=0; i < num_of_plugins; i++) {
var list_number=i+1;
document.write("<font color=red>Plug-in No." + list_number + "- </font>"+navigator.plugins[i].name+" <br>[Location: " + navigator.plugins[i].filename + "]<p>");
}
</script>
</body>
</html>
Thanks
谢谢
回答by funerr
Without jQuery (raw JavaScript):
没有 jQuery(原始 JavaScript):
var data = "...";// this is your data that you want to pass to the server (could be json)
//next you would initiate a XMLHTTPRequest as following (could be more advanced):
var url = "get_data.php";//your url to the server side file that will receive the data.
var http = new XMLHttpRequest();
http.open("POST", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);//check if the data was received successfully.
}
}
http.send(data);
Using jQuery:
使用jQuery:
$.ajax({
type: 'POST',
url: url,//url of receiver file on server
data: data, //your data
success: success, //callback when ajax request finishes
dataType: dataType //text/json...
});
I hope this helps :)
我希望这有帮助 :)
More info:
更多信息:
回答by Trinh Hoang Nhu
Do you know jQuery? It will be much easier with jQuery.
你知道 jQuery 吗?使用 jQuery 会容易得多。
var data = "";
for (var i=0; i < num_of_plugins; i++) {
var list_number=i+1;
document.write("<font color=red>Plug-in No." + list_number + "- </font>"+navigator.plugins[i].name+" <br>[Location: " + navigator.plugins[i].filename + "]<p>");
data += "<font color=red>Plug-in No." + list_number + "- </font>"+navigator.plugins[i].name+" <br>[Location: " + navigator.plugins[i].filename + "]<p>";
}
$.post('savedata.php', {data=data}, function(){//Save complete});
Then in savedata.php you can write something like the following:
然后在 savedata.php 中,您可以编写如下内容:
$data = $_POST['data'];
$f = fopen('file', 'w+');
fwrite(f, $data);
fclose($f);
回答by thommie
Do a request from javascript to a page which runs server side code.
从 javascript 向运行服务器端代码的页面发出请求。
Send post request with ajax http://www.javascriptkit.com/dhtmltutors/ajaxgetpost.shtmlto for example an apsx page. From aspx you could save it to a text file or database.
使用 ajax http://www.javascriptkit.com/dhtmltutors/ajaxgetpost.shtml发送 post 请求到例如 apsx 页面。从 aspx,您可以将其保存到文本文件或数据库。

