简体   繁体   English

无法使用发布请求 nodejs 重定向

[英]cannot redirect using post request nodejs

I have a button on my front end that allows the user to update their profile photo (using Cloudinary api), and I basically call my nodejs post request and pass it the users data so it can query the db and update the profile photo url.我的前端有一个按钮,允许用户更新他们的个人资料照片(使用 Cloudinary api),我基本上调用我的 nodejs 发布请求并将用户数据传递给它,以便它可以查询数据库并更新个人资料照片 url。 works great, but it doesn't redirect / render the page after it calls that function.效果很好,但在调用 function 后它不会重定向/呈现页面。

here's my front end (with Cloudinary and js)这是我的前端(使用 Cloudinary 和 js)

<input type="hidden" name="" id="employee_num" value="<%= data[i].EMPLOYEE_NUM %>">

<script src="https://widget.cloudinary.com/v2.0/global/all.js" type="text/javascript"></script>

<script type="text/javascript">
    var myWidget = cloudinary.createUploadWidget({
            cloudName: 'xxx',
            uploadPreset: 'xxx'}, (error, result) => {
            if (!error && result && result.event === "success") {
                console.log('Done! Here is the image info: ', result.info);
                console.log(result.info.secure_url)
                var result_url = result.info.secure_url;
                console.log("result url is " + result_url)
                document.getElementById("url").value = result_url;
                var employee_num = document.getElementById('employee_num').value;

                fetch('/changeProfileImage', {
                    method: 'POST',
                    headers: {'Content-Type': 'application/json'},
                    body: JSON.stringify({
                        result_url,
                        employee_num
                    })
                })
            }
        }
    )

    document.getElementById("upload_widget").addEventListener("click", function(){
        myWidget.open();

    }, false);
</script>

now below is my nodejs function for the post request /changeProfileImage现在下面是我的nodejs function 用于发布请求/changeProfileImage

app.post('/changeProfileImage', (req, res) => {
    if (req.session.loggedin) {
        var employee_num = req.body.employee_num;
        var url = req.body.result_url;
        console.log("e " + employee_num)
        console.log("u " + url)

        var changeProfileImage = "update EMPLOYEES set (PROFILE_IMAGE)= '" + url + "' where EMPLOYEE_NUM = '" + employee_num + "'";
        ibmdb.open(ibmdbconnMaster, function (err, conn) {
            if (err) return console.log(err);
            conn.query(changeProfileImage, function (err, rows) {
                if (err) {
                    console.log(err);
                }

                res.redirect('/account');

                conn.close(function () {
                });
            })
        })
    } else {
        res.render('login.ejs')
    }
})

forgive me for the messy code, but above is everything that is needed.原谅我乱七八糟的代码,但以上就是所需要的一切。 it basically says in my console that it updated everything, and successfully went through, which is true, but it never refreshes.它基本上在我的控制台中说它更新了所有内容,并成功通过了,这是真的,但它从不刷新。 any ideas?有任何想法吗?

The thing is you are making a AJAX call so the js is making the HTTP request to the server and it is not the whole page, so you need yo do it manually in the callback of the fetch:问题是您正在进行 AJAX 调用,因此 js 正在向服务器发出 HTTP 请求,它不是整个页面,因此您需要在获取的回调中手动执行此操作:

fetch('/changeProfileImage', {
                    method: 'POST',
                    headers: {'Content-Type': 'application/json'},
                    body: JSON.stringify({
                        result_url,
                        employee_num
                    })
}).then(response => {
    if (response.redirected) {
        window.location.href = response.url;
})

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

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