我有一個表單,當點擊提交button
通過ajax
撥打電話。這個ajax在php文件裏面,因爲我需要用數據庫中的數據填充一些變量。但是我無法在成功之前使用這些電話。他們只是不工作,我做了試圖返回一些data
,使用alert
,console.log
並沒有發生任何反應。有趣的是,如果ajax在文件js
中被隔離,它們就可以工作。有人可以幫助我嗎?Ajax調用(before/success)在php文件中不起作用
文件:
<?php
$var = 'abc';
?>
<script type="text/javascript">
$(document).ready(function() {
$('#buy-button').click(function (e){
var abc = '<?php echo $var; ?>';
$.ajax({
type: 'POST',
data: $('#buy-form').serialize(),
url: './ajax/buy_form.php',
dataType: 'json',
before: function(data){
console.log('ok');
},
success: function(data){
},
});
});
});
</script>
HTML:
<form id="buy-form">
<div class="regular large gray">
<div class="content buy-form">
/* some code here */
<div class="item div-button">
<button id="buy-button" class="button anim" type="submit">Comprar</button>
</div>
</div>
</div>
</form>
---- 編輯 ----
問題解決了!錯誤發生在before
ajax中。正確的術語是beforeSend
而不是before
。謝謝大家的幫助。
是你的URL參數是否正確?是不是隻是'/buy_form.php'? –
如果你有一個提交按鈕,它會**默認**提交頁面,因此它將重新加載它,因此ajax請求將無法按預期工作。您能否向我們提供關於HTML端和文件夾層次結構的更多信息?此外,您在這裏有一個錯誤:'成功:功能(數據){'}。 < - 刪除最後一個逗號。 – briosheje
@briosheje除非OP使用的是真正舊的IE,否則尾隨逗號不會導致錯誤 – epascarello