Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

768
Views
XMLHttpRequest lanza InvalidSateError diciendo "El estado del objeto debe abrirse"

El código -

 "use strict"; var AJAX = function (params) { this.server ={}; this.url = params.url; this.method = params.method; this.dataType = params.dataType; this.formData = params.formData; this.init = function(){ if(typeof XMLHttpRequest != 'undefined'){ this.server = new XMLHttpRequest(); this.server.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); this.server.setRequestHeader('Content-length', this.formData.length); this.server.setRequestHeader('Connection', 'close'); console.log("XMLHttpRequest created."); return true; } }; this.send = function(){ if(this.init()){ this.server.open(this.method, this.url, true); this.server.send(this.formData); } }; };

Está arrojando el siguiente error:

 Error in event handler for contextMenus: InvalidStateError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': The object's state must be OPENED.

Cómo se está utilizando -

 var data = new FormData(); data.append('user', 'sachin'); var params = { url : 'example.com', method : 'post', dataType: 'json', formData : data }; var backgroundWindow = chrome.extension.getBackgroundPage(); var ajax = new backgroundWindow.AJAX(params); ajax.send();

Parece que no puedo entender cuál es la razón detrás.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

El error es sencillo:

Error en el controlador de eventos para contextMenus: InvalidStateError: no se pudo ejecutar 'setRequestHeader' en 'XMLHttpRequest': el estado del objeto debe estar ABIERTO.

Debe llamar a .open(..) antes de configurar los encabezados de solicitud.



Dado su código, creo que la mejor manera sería mover la llamada para que se abra en la función init(..) .

 var AJAX = function (params) { this.server ={}; this.url = params.url; this.method = params.method; this.dataType = params.dataType; this.formData = params.formData; this.init = function(){ if(typeof XMLHttpRequest != 'undefined'){ this.server = new XMLHttpRequest(); //Open first, before setting the request headers. this.server.open(this.method, this.url, true); //Now set the request headers. this.server.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); //this.server.setRequestHeader('Content-length', this.formData.length); //this.server.setRequestHeader('Connection', 'close'); console.log("XMLHttpRequest created."); return true; } }; this.send = function(){ if(this.init()){ this.server.send(this.formData); } }; };
over 4 years ago · Santiago Trujillo Report

0

Probablemente necesite abrir la conexión después de la llamada XMLHttpRequest y antes de la llamada setRequestHeader En lugar de:

 this.server = new XMLHttpRequest(); this.server.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

creo que tienes que hacer:

 this.server = new XMLHttpRequest(); this.server.open(this.method, this.url, true); this.server.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

también asegúrese de eliminar esta línea en init() , como lo hizo anteriormente.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!