cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X

Other alternative to xmlhttp

leonardosella
13-Aquamarine

Other alternative to xmlhttp

Hi everyone, 

I have a file.txt that is written with json rules. Into my project I want to access the data of the file like a simply array. 

 

Is there other way to open this txt file?

 

My code is the following one: 

 

var xmlhttp = new XMLHttpRequest(); //apertura del file sequenze
xmlhttp.onreadystatechange = function() {
	if (this.readyState == 4 && this.status == 200) {
		myObj = JSON.parse(this.responseText)
        console.log(myObj.repair_sequence) // repair_sequence è il nome dell'oggetto indicato nel file .txt formattato con grammatica json
		
      	
      	$scope.updateLabel = function(){   //parameter viene passato quando si seleziona la lingua nei rispettivi pulsanti
        	console.log("----STEP STARTED----")
         
      		$scope.$on("stepcompleted", function () { //questa chiamata attende il completamento del passo prima di azzerare la stringa "text"
            	console.log("----STEP COMPLETED----")	
          		text=""
				$scope.setWidgetProp( "stepDescription", "text", text)  //step-description è il nome della label in cui di vanno a stampare le indicazioni dei passi 
			});
         
        	$scope.$on('stepstarted', function() {
         		var currentStep = $scope.app.view.Maintenance.wdg[nomeVista].currentStep //nomeVista corrisponde al nome della vista creata nel canvas
		 		console.log(currentStep)
         		var repair_selection = $scope.getWidgetProp(nomeVista, 'sequence')
               	if(repair_selection == sequence_1)
            		sequence_type=0
        		else if(repair_selection == sequence_2)
                		sequence_type=1
        		else 
         			console.log("Error!!!")
 		 
          		// stampa descrizione sequenza di manutenzione 
    			// traino[0]: italiano
              	//traino[1]: inglese
          		text =  myObj.repair_sequence[sequence_type].traino[0].description[currentStep-1];
                $scope.setWidgetProp( "stepDescription", "text", text)        
          		}); 

        }
      
    }       
};

 

 

 

1 REPLY 1

Hi @leonardosella ,

I think the solution in this case is always similar:

- you will try to read the file into session

-you will try to interpret the file.

For example, if we have a json file (because the extension should still point of the content of the file) - txt file could be any thing:

 

{"button-1":{"class":"button1"},"button-2":{"class":"button1"},"button-3":{"class":"button1"},"3DLabel-1":{"visible":false,"text":"This is PTC Remote Control Model"}

 

 

This should be some properties setting for different widgets

Then we have to use some syntax like this

The main frame calling the code which read the file. Here used the event after loading the 2d view

 

 

$scope.jsonData={}; //global $scope variable
$scope.Init=function() {
doRead('app/resources/Uploaded/'+pjson);
}
/////
$scope.$on('$ionicView.afterEnter',function(){
  console.log("$ionicView.afterEnter was called");
$scope.Init();}) //event: when 2d View loaded

 

 

Then here the function for loading the file - used the fetch construct:

 

 

//this is the function which handle the json data
gotJSON=function(data){
 try{
$scope.jsonData=JSON.parse(data);
 console.warn(  JSON.stringify($scope.jsonData))
   $scope.setMutipleProps($scope.jsonData)
 } 
catch(wrong){    console.warn(wrong);}

}
//this is the function which read the file
doRead=function (jsonFile){
fetch(jsonFile)
  .then(response=>response.text())
  .then(data=>gotJSON(data))
}

 

 

There is also a sample implementation of the function setMultipleProp - so set the array to widget properties:

 

 

//the code will read the complette JSON File and
//assignee it to a jsonData global variable
//func will set mutliple widget properties according JSON
$scope.setMutipleProps  = function(obj){

 Object.keys(obj).forEach(function (item)  {
                for(var prop in obj[item])    {
                           $scope.view.wdg[item][prop]=obj[item][prop];  
                           //print the setting for debugging                 
							console.log("==>$scope.view.wdg["+item+"]["+prop+"]="+obj[item][prop] )
                                              } 
                							})
                                       };

 

Announcements

Top Tags