拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 ajax通过电报api重定向到另一个页面

ajax通过电报api重定向到另一个页面

白鹭 - 2022-02-16 1999 0 0

我遇到了 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");
            }
        });
    }
});

uj5u.com热心网友回复:

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

uj5u.com热心网友回复:

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

var url = base_url   "sample.html";
window.location.href = url;
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *