2

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>
<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

4

3 回答 3

6

没有 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", data.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);

使用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...
});

我希望这有帮助 :)

更多信息:

于 2012-06-28T08:50:33.380 回答
1

你知道 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});

然后在 savedata.php 中,您可以编写如下内容:

$data = $_POST['data'];
$f = fopen('file', 'w+');
fwrite(f, $data);
fclose($f);
于 2012-06-28T08:52:01.743 回答
0

从 javascript 向运行服务器端代码的页面发出请求。

使用 ajax http://www.javascriptkit.com/dhtmltutors/ajaxgetpost.shtml发送发布请求到例如 apsx 页面。您可以从 aspx 将其保存到文本文件或数据库中。

于 2012-06-28T08:47:41.760 回答