Use POST instead of GET to exchange info with webservers and load ascii data with the correct mimetype.

This commit is contained in:
2023-04-07 16:15:49 +02:00
parent df3fe3e2a4
commit e629ed0cb4
+52 -7
View File
@@ -4,26 +4,70 @@ function writeAsciiFile(filename,content) {
// Write string content into ascii file filename
// Go via CGI script
//console.log(filename, content);
// Prepare CGI args
let cgiargs = "?fn="+filename;
// Prepare CGI args use POST for long files
let cgiargs = "fn="+filename; // POST
let lines = content.split(/\n/);
let prefix = filename.split(/\//);
for (let i=0; i<lines.length; i++) {
cgiargs += "&line" + i + "=" + lines[i];
cgiargs += "&line" + i + "=" + lines[i].replace(/\s\s+/g, ' ');
}
var xhttp;
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
//console.log("resptext=",xhttp.responseText);
console.log("response=",xhttp.responseText);
} else if (this.readyState == 4) {
console.log(xhttp.status, xhttp.statusText);
}
}
let request = "/cgi-bin/singleTrimSP.cgi"+cgiargs;
xhttp.open("GET", request, false);
xhttp.send();
let request = "/cgi-bin/singleTrimSP.cgi"; //POST
xhttp.open("POST", request, false); //POST
//Send the proper header information along with the request
xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); //POST
xhttp.send(cgiargs); //POST
// Now you can add a link to the files
let d = document.getElementById("linkDiv");
if (!d) {
d = document.createElement("div");
d.id = "linkDiv";
document.body.appendChild(d);
}
let htmlLink = "Download files: <a href='";
htmlLink += "/tmp/" + prefix[2] + ".tgz'>";
htmlLink += prefix[2] + ".tgz</a>";
d.innerHTML = htmlLink;
return(1);
}
function writeAsciiFile2(filename,content) {
// Write string content into ascii file filename
// Go via CGI script
//console.log(filename, content);
// Prepare CGI args use GET for short files
let cgiargs = "?fn="+filename; // GET
let lines = content.split(/\n/);
let prefix = filename.split(/\//);
for (let i=0; i<lines.length; i++) {
cgiargs += "&line" + i + "=" + lines[i].replace(/\s\s+/g, ' ');
}
var xhttp;
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log("response=",xhttp.responseText);
} else if (this.readyState == 4) {
console.log(xhttp.status, xhttp.statusText);
}
}
let request = "/cgi-bin/singleTrimSP.cgi"+cgiargs; // GET
xhttp.open("GET", request, false); //GET
//Send the proper header information along with the request
xhttp.send(); // GET
// Now you can add a link to the files
let d = document.getElementById("linkDiv");
if (!d) {
@@ -60,6 +104,7 @@ function readAsciiFile(filename) {
filename += "?" + Date();
//filename = "http://musruser.psi.ch" + filename;
xhttp.open("GET", filename, false);
xhttp.overrideMimeType("text/plain");
xhttp.send();
return(xhttp.responseText);
}