简体   繁体   English

Response.redirect() 不是 vanilla JS 中的函数?

[英]Response.redirect() is not a function in vanilla JS?

I am trying to redirect to another page after I receive a response from the post fetch, but as the title says it doesn't work.在收到帖子提取的响应后,我试图重定向到另一个页面,但正如标题所说,它不起作用。

These are the functions:这些是函数:

// 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);
        });
}

Every console.log();每个console.log(); returns Response {type: 'basic', url: 'http://192.168.0.100/dashboard', redirected: true, status: 200, ok: true, …}返回Response {type: 'basic', url: 'http://192.168.0.100/dashboard', redirected: true, status: 200, ok: true, …}

If I place response.redirect(response.status, response.url);如果我放置response.redirect(response.status, response.url); in the first then() I get the same error.在第一个then()我得到同样的错误。

So, does response.redirect exist in Vanilla JS ?那么,Vanilla JS 中是否存在response.redirect

I don't want to use window.location.href or any other similar option because it bypasses HTTP Authentication header.我不想使用window.location.href或任何其他类似的选项,因为它绕过了 HTTP 身份验证标头。

I see that you have the 'follow' argument given in the fetch.我看到您在提取中给出了“跟随”参数。

You can check the if the response is being redirected using the code below.您可以使用下面的代码检查响应是否被重定向。 If it was not redirected you can simply change the window location and also force a redirect.如果它没有被重定向,您可以简单地更改窗口位置并强制重定向。

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

EDIT:编辑:

After doing a bit more research into the redirect method I saw that you need to switch the URL and status variables, see: https://developer.mozilla.org/en-US/docs/Web/API/Response/redirect在对重定向方法进行了更多研究后,我发现您需要切换 URL 和状态变量,请参阅: https : //developer.mozilla.org/en-US/docs/Web/API/Response/redirect

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM