skip to Main Content

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

2

Answers


  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");
                }
            });
        }
    });
    
    Login or Signup to reply.
  2. in your success callback, do something like this

    var url = base_url + "sample.html";
    window.location.href = url;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search