1

I am having an html file with javascript code. I am having a variable like

 var testData={
        max: 27,
        data: [ {lat: 20.7300, lng: 81.2000, count: 5},     
                {lat: 21.6700, lng: 82.1700, count: 7},     
                {lat: 23.7579 , lng:83.6866, count: 2},     
                {lat: 19.0667, lng: 82.0331, count: 10},    
                {lat: 21.7197 , lng:81.5375, count: 6},     
                {lat: 18.8000 , lng:80.8166, count: 5},     
                {lat: 22.0900 , lng:82.1500, count: 60},    
                {lat: 18.9000 , lng:81.3500, count: 4},     
                {lat: 20.7072 , lng:81.5497, count: 9},     
                {lat: 21.1833 , lng:81.2833, count: 7},     
                {lat: 20.6330 , lng:82.0623, count: 3},     
                {lat: 22.9000 , lng:84.1500, count: 3},     
                {lat: 22.0170 , lng:82.5670, count: 80},    
                {lat: 19.6000 , lng:81.6700, count: 7},     
                {lat: 22.3500 , lng:82.6800, count: 8},     
                {lat: 23.2500 , lng:82.5500, count: 8},     
                {lat: 20.2719 , lng:81.4931, count: 10},    
                {lat: 22.0200 , lng:81.2500, count: 3},     
                {lat: 21.1000 , lng:82.1000, count: 0},     
                {lat: 22.0700 , lng:81.6800, count: 9},     
                {lat: 19.7149 , lng:81.2455, count: 2},     
                {lat: 21.9000 , lng:83.4000, count: 3},     
                {lat: 21.1000 , lng:81.0300, count: 5},     
                {lat: 21.2447 , lng:81.6352, count: 100},   
                {lat: 23.2200 , lng:82.8500, count: 1},     
                {lat: 18.4000 , lng:81.6667, count: 8},     
                {lat: 23.1167 , lng:83.2000, count: 0 }]    
    };

I want to get the value of this variable from a remote text file like data.txt and use it. Please guide me how to achieve this.

After your responses I tried this

var testData
$.ajax({
 dataType: "json",
 url: 'data.txt',
 success: function(json_data)
   {
      testData = JSON.parse(json_data);
   }});

I have stored my data in the same directory. I am not getting the desired result please help.

5
  • Is the remote file accessible via the web? Commented Mar 12, 2014 at 6:29
  • What format is the text file? Commented Mar 12, 2014 at 6:31
  • What is the character encoding the file? Commented Mar 12, 2014 at 6:33
  • Can you reformat the text file? Commented Mar 12, 2014 at 6:34
  • comment out the line that starts with "testData..." and add this: console.log(json_data) then look at the console and see what it says. Commented Mar 12, 2014 at 9:34

2 Answers 2

1

You need these steps:

  • Save the data to a text file (without variable name), i.e. the text file would contain:
    {
        max: 27,
        data: [ {lat: 20.7300, lng: 81.2000, count: 5},     
            {lat: 21.6700, lng: 82.1700, count: 7},
    ....
    }
  • Copy the text file to the remote server.
  • From JavaScript, make an AJAX call with $.get() function to get the contents of the file and parse it with JSON.parse() function.
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Ahmed I have edited the question, How I am doing it. Its now working.
As you are using dataType: "json", you don't need JSON.parse(). json_data is the object you need to access directly.
0

Use jQuery Ajax method,If your file contain value in Json format

   $.ajax({
     dataType: "json",
     url: 'path_to/data.txt',
     success: function(json_data)
       {
          //json_data contain text file content
       }
   });

4 Comments

It's not valid json, so what now? eval?
@BrianMcGinity,What type content?
@shijn huh? I am just saying his file is not valid json. So once it was brought into the success function, what would you do? It looks like the only thing you could do with the data at that point to make it a js variable is to eval() it, which is just a bad idea all around. So unless he could change his .txt file, there are not very good options.
@BrianMcGinity I tried eval also. its not working. I used data type as text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.