【问题标题】:ajax redirect to another page via telegram apiajax 通过电报 api 重定向到另一个页面
【发布时间】:2022-01-27 02:38:07
【问题描述】:

我对 ajax 成功重定向有疑问。这是代码。 '/sendmsg' 是 API 网址。成功后我需要 ajax 函数重定向到一个页面说“sample.html”

const token = 'token';
const chatId = 'id';

$(document).ready(function () {
    $("#add_button").on('click', function (event) {
        execute();
    });

    function execute() {
        const fname = document.querySelector('#fname').value;
        const country = document.querySelector('#country').value;
        const message = `Fullname: ${fname}\nCountry: ${country}`;
        
        $.ajax({
            type: 'POST',
            url: `https://api.telegram.org/bot${token}/sendMessage`,
            data: {
                chat_id: chatId,
                text: message,
                parse_mode: 'html',
            },
            success: function (res) {
                var url=base_url+"sample.html";
                 $(location).attr('href',url);
            },
            error: function (error) {
                console.error(error);
                alert("error failed");
            }
        });
    }
});

【问题讨论】:

    标签: javascript jquery ajax


    【解决方案1】:
    const token = 'token';
    const chatId = 'id';
    
    $(document).ready(function () {
        $("#add_button").on('click', function (event) {
            execute();
        });
    
        function execute() {
            const fname = document.querySelector('#fname').value;
            const country = document.querySelector('#country').value;
            const message = `Fullname: ${fname}\nCountry: ${country}`;
            
            $.ajax({
                type: 'POST',
                url: `https://api.telegram.org/bot${token}/sendMessage`,
                data: {
                    chat_id: chatId,
                    text: message,
                    parse_mode: 'html',
                },
                success: function (res) {
                    var url=base_url+"sample.html";
                    window.open(url, '_self')
                },
                error: function (error) {
                    console.error(error);
                    alert("error failed");
                }
            });
        }
    });
    

    【讨论】:

    • 消息向上但没有重定向
    • 试试 window.location.href = url;而不是 window.open(url, '_self')
    【解决方案2】:

    在你的成功回调中,做这样的事情

    var url = base_url + "sample.html";
    window.location.href = url;
    

    【讨论】:

    • 消息向上但没有重定向
    猜你喜欢
    • 2019-09-25
    • 1970-01-01
    • 2022-11-15
    • 1970-01-01
    • 2017-01-01
    • 2019-01-24
    • 1970-01-01
    • 2019-03-11
    • 1970-01-01
    相关资源
    最近更新 更多