Me gustaría crear una función que cree la estructura de un XMLHttpRequest deseado y la genere como una variable que puedo usar. He intentado esto con el siguiente código:
function prepareXMLHttpRequest(type) {
var targetURL = 'http://localhost:3000/';
var xhr = new XMLHttpRequest();
xhr.open(type, targetURL, true);
xhr.setRequestHeader('Content-Type', 'text/plain');
//set any other options here
return xhr;
}
Mi intención es llamar a esa función con GET o POST y, a continuación, enviar el resultado:
prepareXMLHttpRequest("GET").then(function(getRequest) {
getRequest.send();
// Do something here, for example console.log the output
})
prepareXMLHttpRequest("POST").then(function(postRequest) {
postRequest.send(outputString);
})
Cuando intento cualquiera de estas declaraciones, recibo el siguiente error:
PrepareXMLHttpRequest(...).then is not a function
¿Qué estoy haciendo mal?
1
Chris
5 nov. 2019 a las 18:13
Nuevas preguntas
javascript
Para preguntas sobre la programación en ECMAScript (JavaScript / JS) y sus diversos dialectos / implementaciones (excepto ActionScript). Incluya todas las etiquetas relevantes en su pregunta; por ejemplo, [node.js], [jquery], [json], etc.