• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

461
Views
Response.redirect () no es una función en Vanilla JS?

Estoy tratando de redirigir a otra página después de recibir una respuesta de la búsqueda de la publicación, pero como dice el título, no funciona.

Estas son las funciones:

 // send/post json async function postData(json_data, api_path) { const response = await fetch(api_path, { method: "POST", headers: { Accept: "application/json", "Content-Type": "application/json", }, body: json_data, redirect: 'follow' }); console.log("postData response: ", response); return response; } // send JSON data to server on /api/${destination} function saveSettings(form, destination) { let json_data = toJSONstring(form); let res; console.log(json_data); postData(json_data, `/api/${destination}`) .then((response) => { res = response; if (!response.ok) { throw new Error(`HTTP error, status = ${response.status}`); } return response.text(); }).then(text => { if (destination === 'network/post') { connected = false; updatingToast(`You are no longer connected to the device !`, false); updatingToast(`Please navigate to ${text}`, true, text); } console.log('res: ', res); res.redirect(res.status, res.url); }); }

Cada console.log(); devuelve Response {type: 'basic', url: 'http://192.168.0.100/dashboard', redirected: true, status: 200, ok: true, …}

Si coloco response.redirect(response.status, response.url); en el primero then() obtengo el mismo error.

Entonces, ¿ responsable.redirect existe en Vanilla JS?

No quiero usar window.location.href ni ninguna otra opción similar porque omite el encabezado de autenticación HTTP.

almost 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Veo que tiene el argumento 'seguir' dado en la búsqueda.

Puede verificar si la respuesta se está redirigiendo usando el código a continuación. Si no fue redirigido, simplemente puede cambiar la ubicación de la ventana y también forzar una redirección.

 if (res.redirected) { window.location.href = res.url; }

EDITAR:

Después de investigar un poco más sobre el método de redirección, vi que necesita cambiar la URL y las variables de estado, consulte: https://developer.mozilla.org/en-US/docs/Web/API/Response/redirect

almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error