【发布时间】: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");
}
});
}
});