Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法执行$.ajax中的成功_Javascript_Php_Jquery_Html_Ajax - Fatal编程技术网

Javascript 无法执行$.ajax中的成功

Javascript 无法执行$.ajax中的成功,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我是ajax的新手。作为第一个示例,我想实现一个add操作。为此,我编写了以下代码: Html: 问题是,当我取消注释//警报(“Wfah”)时,我在ajax成功中获得结果,然后重定向到add.php。当我没有取消注释它时,我直接被重定向到add.php。看来成功不算什么。 请帮帮我。还建议您成为学习ajax的好来源。谢谢。$('addForm')。关于('submit', $('#addForm').on('submit', function (e) { e.preve

我是ajax的新手。作为第一个示例,我想实现一个add操作。为此,我编写了以下代码:

Html:

问题是,当我取消注释//警报(“Wfah”)时,我在ajax成功中获得结果,然后重定向到add.php。当我没有取消注释它时,我直接被重定向到add.php。看来成功不算什么。 请帮帮我。还建议您成为学习ajax的好来源。谢谢。

$('addForm')。关于('submit',
$('#addForm').on('submit',
    function (e) {
        e.preventDefault(); //<----- you need to prevent the form from submitting
        // alert("hello submit");

        var contents = $(this).serialize(); 

        $.ajax(
        {
            url:'add.php',
            dataType: 'json',
            type:'post',
            data:contents,
            success:function(data)
            {
                if(data.success)
                {
                    alert("result is " + data.result);
                }
            }
        }
            );
        // alert("Wfah");
    });
职能(e){
E.PrimeDebug();//< p>您也在PHP代码段中设置<代码> $JSON [成功] <代码> >“true”<代码>,这可能只是一个较小的逻辑错误,但是考虑将其更改为布尔<代码> true < /Cord>:
$json['success'] = "true";
进入


它起作用了。你能解释一下吗?另外,请告诉我jquery的好来源。我没有15个声誉,否则我会投票。事件通常有一个与之相关的默认操作。例如,上面的表单有一个默认操作提交。当你使用
preventDefault()时
,您阻止执行默认操作。有关更多信息,@kmcnamee?:P我找不到您?好的,找到您了。这是一个很好的笑话。给予和索取..我一般不会对我的问题进行投票。我相信,由于ajax是异步的,您的警报(“Wfah”),使您能够看到“ajax成功警报”所需的延迟。这就是为什么在注释掉它时,您看不到“ajax成功警报”,因为重定向到php脚本已经发生。
$('#addForm').on('submit',
    function () {
        // alert("hello submit");

        var contents = $(this).serialize(); 

        $.ajax(
        {
            url:'add.php',
            dataType: 'json',
            type:'post',
            data:contents,
            success:function(data)
            {
                if(data.success)
                {
                    alert("result is " + data.result);
                }
            }
        }
            );
        // alert("Wfah");
    });
$('#addForm').on('submit',
    function (e) {
        e.preventDefault(); //<----- you need to prevent the form from submitting
        // alert("hello submit");

        var contents = $(this).serialize(); 

        $.ajax(
        {
            url:'add.php',
            dataType: 'json',
            type:'post',
            data:contents,
            success:function(data)
            {
                if(data.success)
                {
                    alert("result is " + data.result);
                }
            }
        }
            );
        // alert("Wfah");
    });
$json['success'] = "true";
$json['success'] = true;