简体   繁体   中英

How to change POST request

I want to make post request in html in iframe, but after this post request user automatically redirect to request page. I want to make just request, without redirect.

<form id="main-mailing-list" action="https://api.telegram.org/botXXXXXXXX/sendMessage" method="POST" enctype="multipart/form-data" 
  <input type="hidden" name="chat_id" value="-44947">
  <input tabindex="0" autocomplete="email" id="emailfield" data-validation="email" class="required email" name="text" type="email" placeholder="Your email address"> 
  <button id="submit" type="submit">SUBMIT</button><br><br>
  <span id="thanks"></span>
</form>

In jQuery you can do it like this:

$("#submit").on("click", function(e){
    e.preventDefault();  //prevent from default behavior

    var email = $("#emailfield").val();
    var chat_id = $("#main-mailing-list [name='chat_id']").val();

    var data = {
        email: email,
        chat_id: chat_id
    };

    $.ajax({
        method: "POST",
        url: "https://api.telegram.org/botXXXXXXXX/sendMessage",
        data: data
    }).done(function(response){
       console.log(response)

    }).fail(function(error){
       console.error(error)
    });
});

Of course, if something will not work as expected it is very likely that headers are not set correctly. More info about it here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM