0

file location Ex: http://localhost:8080/config/userlog.conf (file which having json data)

Below function is for read data from above file.

JSON Data in above file: {"datetime": "01/10/2018 16:33:20", "password": "password123", "username":"vk@example.com"}

var getConfigData = function(filename) {
  var data = {
    conf: filename,
    what: 'conf'
  };
  return $.ajax({
    url: '/config/userlog.conf',
    data: data,
    global: false
  });
};
var logData = getConfigData('userlog.conf');
console.log(logData); //output getting undefined

Question: how to read data from .conf file type

  • $.ajax will return a promise. You could access the value inside the success function. Refer this ajax doc: http://api.jquery.com/jquery.ajax/ – dRoyson Oct 01 '18 at 12:05
  • now I'm able to get data after success callback, just adding .done() after my function call. Thanks for the info @Royson – Vinay Kumar Oct 03 '18 at 05:39
  • Just one more question, how we can add data (above json format for example) to above file type on every time (assume that, data is collecting on every login attempt with same json format). That file should contain multiple objects (json objects). – Vinay Kumar Oct 03 '18 at 06:08
  • I didn't get you. Could you elaborate further or edit the question and add the code if its related or create a new question with the code and link it – dRoyson Oct 03 '18 at 08:56
  • userlog.conf file is having this data: [ { "userName": "info1@gmail.com", "dateTime": "03/10/2018 11:12", "password": "info123", }, { "userName": "info2@gmail.com", "dateTime": "13/09/2018 02:35", "password": "info234", } ]; how can I add new object into this file? – Vinay Kumar Oct 03 '18 at 10:03
  • I don't think it is possible to post your data directly to file. Ideally, it should be handled by the server. You can try sending the following request : `$.ajax({ url: '/config/userlog.conf', data: data, global: false, method: "POST" })` Let me know if this works for you. – dRoyson Oct 04 '18 at 04:55
  • No, this is not working. – Vinay Kumar Oct 04 '18 at 13:06
  • Even I had my doubts that it won't work. You need a server side code that will handle the POST request and make the changes to the file. – dRoyson Oct 04 '18 at 13:11
  • Can I try with file blob constructor? – Vinay Kumar Oct 04 '18 at 13:15

2 Answers2

0
$.ajax({url: "/config/userlog.conf"}).done(function(data) {console.log(data);});
MThiele
  • 387
  • 1
  • 9
0

The ajax request is assigned to logData variable.

You should define it earlier in the code and assign it in the "success" field of the request.

var logData;
var getConfigData = function(filename) {
    return $.ajax({
        url: '/config/'+filename,
        success: data => logData = data,
        global: false
    });
};
getConfigData('userlog.conf');
console.log(logData);