[英]JQuery multiple POSTs on form submission
我正在嘗試將表單提交到2個不同的腳本提交表單。 提交表單的ScriptA在提交時重定向。 ScriptB不給出任何形式的響應,只是將給定的信息寫下來。
我無法使提交工作正常,而且我對JQuery也不是很有經驗,這是我第一次來,所以我希望有人可以指出為什么我的表單無法使用JQUERY正確提交。
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>
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>
我遇到的一個離題問題是如何解決代碼的格式? 每當我在這里發布代碼時,我總是會縮進。
$(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);
}
});
});
});
您的代碼有一些問題。 為什么不綁定到表單的submit
事件,而不是綁定到提交按鈕的click
事件。 同樣,當您序列化表單時,要定位表單,並選擇$('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
});
您的第二個$.post()
似乎將表單提交發送到用戶當前所在的其他域。 您只能使用JSONP
做到這一點: http : //api.jquery.com/jquery.ajax (搜索JSONP
,您將找到有關如何執行此操作的出色信息)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.