Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

jquery - submit to multiple urls

I have an html form with 2 fields: name and email. I need to submit the form values to 2 different urls from the same submit button. I have tried using jquery, serialize and post but I am not clear how to accomplish this. I cannot use PHP in this instance.

<!doctype>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

</head>
<body>

<form action="" method="post" name="myform" id="myform">

<input type="text" value="name" />
<input type="email" value="email" />
<input type="button" id="submit" value="submit" />
</form>

<script type="text/javascript">
$(document).ready(function(){
    var url1 = "some-url.com";
    var url2 = "some-other-url";
   $("#submit").click(function() {
        $.post("url1", $("form#myform").serialize());
    $.post("url2", $('formmy#myform').serialize());
    })
})
</script>
</body>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you're using jquery:

$( "form" ).submit(function( event ) {
  submitToURL1();
  submitToURL2();
  //something else
  alert( "Handler for .submit() called." );
  event.preventDefault(); //so that it doesn't get suimitted again
});

submitToURL1 could be something like:

$.post("url1", data : $("form").serialize());

.serialize() is a jquery function that takes the elements in a form and arranges them nicely so you can send them as params to a post method, for instance. https://api.jquery.com/serialize/

More information here: https://api.jquery.com/submit/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...