我有JSP的本地网页,我可以通过此页面通过电报机器人API向电报用户发送消息或发送照片。但是在从我的页面提交此请求后,URL会重定向到另一个页面,该页面返回那里显示的电报Bot API方法的值。我想获取这些参数并在我的本地页面上返回值,并且我不想转到该URL。
这是例如:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head></head>
<body>
<form action="https://api.telegram.org/bot<token>/sendMessage" method="POST">
<input type="text" name="chat_id" id="chat_id"value="myChatId">
<input type="text" name="text" id="text" value="hello">
<input type="submit" value="submit">
</form>
</body>
</html>
当我点击提交时,会出现此页面:
https://api.telegram.org/bot<Token>/sendMessage并向我显示请求的结果,并通过myChatId的Id将hello发送给用户。所以,我希望这个页面不显示,我将在我当前的本地页面上,但我可以在本地页面上接收并查看结果并返回通过此方法发送消息的参数。
答案 0 :(得分:1)
您可以使用本地页面中的ajax请求通过post方法和阻止页面重新加载来发送api url:
$('form').submit(function(e) {
e.preventDefault();
$.ajax({
url: 'https://api.telegram.org/bot' + $('#token').val() + '/sendMessage',
method: 'POST',
data: {
chat_id: $('#chat_id').val(),
text: $('#text').val()
},
success: function() {
alert('your message has been sent!');
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input type="text" name='token' id="token" placeholder="your bot token">
<input type="text" name="chat_id" id="chat_id" value="myChatId">
<input type="text" name="text" id="text" value="hello">
<input type="submit" value="submit">
</form>