Using javascript and node.js how to post the content to the text file through ajax -


using javascript , node.js how post content text file through ajax.

<html> <body>      <input type="button" value="click me" onclick="postcontent()">   <script>     function postcontent(){         var req=new xmlhttprequest();         req.open('post','addcontent.js',true);//in sample.txt file want insert content         req.send();       }    </script>  </body> </html> 

addcontent.js

  var fs = require('fs');   fs.writefile("/tmp/sample.txt", "hey there!", function(err) {    if(err) {       console.log(err);    } else {      console.log("the file saved!");    } });  

you can have server-side script writes content text file , call server-side script using ajax.

req.open('post', 'fileposter', true); req.send(datatowrite); 

edited new question:

req.open('post','addcontent.js',true); 

addcontent.js should http server running on host , port.

if addcontent.js node http server, running on localhost , port 8080, open call should be

req.open('post', 'localhost:8080', true); 

Comments

Popular posts from this blog

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

Android Gson.fromJson error -