Ask your JavaScript questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

can't parse External tab-delimited file with $.get JavaScript

  • SOLVED

External tab-delimited file used with $.get below:

Attaya, James J 3
Badamo, Anthony 0 25
Jesse, Brian J 3
Kehoe, Eugene 3
Lionti, Michael P
Nesci, Matthew J 3
Trowbridge, James C 5


<script>
function repop() {

document.getElementById("reg").reset();
sense = document.getElementById("shelter").value; // the date at top of page header
happen = "cccr-data/MS_" + sense.substr(6,4) + "_" + sense.substr(0,2) +"_" + sense.substr(3,2) +".TSV";
$.get(happen, function (response) {topcare = response; alert(topcare);}); // this works fine!

temp = topcare.split('\n'); / I don't think this works But I have to extract the tuples separated by \n.

alert (temp[1]); // This doesn't work.

for (i=1; i<= temp.length-1; i++) {

temper = temp[i];

soul = temper.split('\t');

document.getElementById("P" +i).value = soul[0]; //this shows nothing!
document.getElementById("E" +i).value = soul[1]; //this shows nothing!
document.getElementById("M" +i).value = soul[2]; //this shows nothing!
}
}

</script>

Answers (1)

2019-08-16

Rainner Lins answers:

You have to wait for $.get() to finish getting the file before trying to parse it. Try something like this;



function parseResponse( response ) {
temp = response.split('\n');

for (i=1; i<= temp.length-1; i++) {
temper = temp[i];
soul = temper.split('\t');
document.getElementById("P" +i).value = soul[0];
document.getElementById("E" +i).value = soul[1];
document.getElementById("M" +i).value = soul[2];
}
}

function repop() {
document.getElementById("reg").reset();
sense = document.getElementById("shelter").value; // the date at top of page header
happen = "cccr-data/MS_" + sense.substr(6,4) + "_" + sense.substr(0,2) +"_" + sense.substr(3,2) +".TSV";
$.get( happen, parseResponse );
}