简体   繁体   English

Ajax调用(之前/成功)在php文件中不起作用

[英]Ajax call (before/success) doesn't work inside php file

I have a form that when clicked the submit button makes a call via ajax . 我有一个表单,当单击“提交” button时,它将通过ajax进行呼叫。 This ajax is inside a php file because I need to fill in some variables with data from the database. 这个ajax在一个php文件中,因为我需要用数据库中的数据填充一些变量。 But I can not use the calls before / success. 但是我不能使用之前/成功的电话。 They just do not work, I've done tests trying to return some data , using alert , console.log and nothing happens. 它们只是不起作用,我已经完成了尝试使用alertconsole.log返回一些data测试,但没有任何反应。 Interestingly, if the ajax is isolated within a file js they work. 有趣的是,如果ajax被隔离在文件js它们可以工作。 Some can help me please? 有些可以帮助我吗?

File: 文件:

<?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: 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>

---- EDIT ---- ----编辑----

Problem solved! 问题解决了! The error was in the before ajax. 错误出before ajax before The correct term is beforeSend and not before . 正确的术语是beforeSend而不是before Thank you all for help. 谢谢大家的帮助。

You said it was a submit button and you do not cancel the default action so it will submit the form back. 您说这是一个提交按钮,并且您没有取消默认操作,因此它将回发表单。 You need to stop that from happening. 您需要阻止这种情况的发生。

$('#buy-button').click(function (e){
    e.preventDefault();
    /* rest of code */

Now to figure out why it is not calling success 现在弄清楚为什么它不叫成功

        $.ajax({  
            type: 'POST',
            data: $('#buy-form').serialize(),
            url: './ajax/buy_form.php',
            dataType: 'json',
            before: function(data){
                console.log('ok');
            },
            success: function(data){

            },
            error : function() { console.log(arguments); }  /* debug why */
        });
    });

My guess is what you are returning from the server is not valid JSON and it is throwing a parse error. 我的猜测是您从服务器返回的内容不是有效的JSON,并且抛出了解析错误。

Try this 尝试这个

<script type="text/javascript">
$(document).ready(function() {

    $('#buy-button').click(function (e){
        e.preventDefault();
        var abc = '<?php echo $var; ?>';

        $.ajax({  
            type: 'POST',
            data: $('#buy-form').serialize(),
            url: './ajax/buy_form.php',
            dataType: 'json',
            beforeSend: function(data){
                console.log('ok');
            },
            success:function(data){

            }
        });
    });

});

And make sure that your php file return response 并确保您的php文件返回响应

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

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