Home > Software design >  ajax redirect to another page via telegram api
ajax redirect to another page via telegram api

Time:12-29

I've a problem with ajax success redirect.This is the code. '/sendmsg' is the API url. on success I need the ajax function to redirect to a page say "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");
            }
        });
    }
});

CodePudding user response:

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

CodePudding user response:

in your success callback, do something like this

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