Write javascript output to file on server

后端 未结 3 698
感情败类
感情败类 2020-12-16 08:56

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 sc

3条回答
  •  独厮守ぢ
    2020-12-16 09:20

    Without jQuery (raw 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:

    $.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:

    • https://www.google.co.il/search?q=js+ajax+post&oq=js+ajax+post

    • https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest

提交回复
热议问题