简体   繁体   English

jQuery提交表单时的多个POST

[英]JQuery multiple POSTs on form submission

I am trying to submit my form to 2 different scripts on form submission. 我正在尝试将表单提交到2个不同的脚本提交表单。 ScriptA that the form is submitted to redirects on submission. 提交表单的ScriptA在提交时重定向。 ScriptB doesnt give response of any kind, just writes the given info down. ScriptB不给出任何形式的响应,只是将给定的信息写下来。

I can't get the submission to work and I am not very experienced with JQuery and it is my first time so I was hoping someone could point out why my form isn't properly submitting with JQUERY. 我无法使提交工作正常,而且我对JQuery也不是很有经验,这是我第一次来,所以我希望有人可以指出为什么我的表单无法使用JQUERY正确提交。

Body of Html: HTML正文:

<script type="text/javascript">
$(document).ready( function(){
    $('#submit').click(function(){
    //Send data to the email script
    $.post( 'authenticate.php', $('Auth').serialize(), function(data, textStatus) {
        //data is the result from the script
        alert(data);
    });

    //Send data to the other script
    $.post( 'http://Service3.com/j_security_check', $('Auth').serialize(), function(data, textStatus) {
        //data is the result from the script
        alert(data);
    });
    });
});
</script>

Body of html Form: html表单主体:

<form action=authenticate.php method=post name=Auth id="form" class="appnitro">
 <div class="form_description"><h2>Login</h2></div>
<ul>
    <li id="li_1" >
        <label class="description" for="element_1">Username </label>
        <div>
        <input id="element_1" name="j_username" class="element text medium" type="text" maxlength="255" value=""/>
        </div>
    </li>

    <li id="li_2" >
        <label class="description" for="element_2">Password </label>
        <div>
        <input id="element_2" name="j_password" class="element text medium" type="password" maxlength="255" value=""/>
        </div>
    </li>

    <li class="buttons">
     <input id="saveForm" class="button_text" type="submit" name="submit" id="submit" value="Log In" />
    </li>
</ul>
</form>

One off topic question I have is how do I fix the formatting of my code? 我遇到的一个离题问题是如何解决代码的格式? whenever I post my code here I always get huge indentation. 每当我在这里发布代码时,我总是会缩进。

$(document).ready( function(){

  $('#form').submit(function(){

    $.ajax({
      type: 'POST',
      async: false,
      url: "your_url_1",
      data: $(this).serialize(),
      success: function(data, status, xhr){
        alert('ok');
      },
      error: function(xhr, status, err) {
        alert(status + ": " + err);
      }
    });

    $.ajax({
      type: 'POST',
      async: false,
      url: "your_url_2",
      data: $(this).serialize(),
      success: function(data, status, xhr){
        alert('ok');
      },
      error: function(xhr, status, err) {
        alert(status + ": " + err);
      }
    });

  });

});

You have some issues with your code. 您的代码有一些问题。 Instead of binding to the click event for the submit button, why not bind to the submit event for the form. 为什么不绑定到表单的submit事件,而不是绑定到提交按钮的click事件。 Also when you serialize the form you want to target the form and you were selecting $('Auth') which will try to select any Auth tags (which don't exist). 同样,当您序列化表单时,要定位表单,并选择$('Auth') ,它将尝试选择任何Auth标记(不存在)。

$('#form').submit(function(){
    //Send data to the email script
    $.post( 'authenticate.php', $(this).serialize(), function(data, textStatus) {
        //data is the result from the script
        alert(data);
    });
    return false;//this stops the form from submitting normally
});

Your second $.post() looks like it's sending the form submission to a different domain that the one the user is currently on. 您的第二个$.post()似乎将表单提交发送到用户当前所在的其他域。 You can only do this with JSONP : http://api.jquery.com/jquery.ajax (do a search for JSONP and you will find excellent information on how to do this) 您只能使用JSONP做到这一点: http : //api.jquery.com/jquery.ajax (搜索JSONP ,您将找到有关如何执行此操作的出色信息)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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