我遇到了 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 评论