ajax成功返回0


ajax success returning 0

我需要一个小帮助。我已经设计了我的自定义ajax功能。这是一个简单的表单,它输入5个值来验证它们,然后通过ajax将数据发送到php函数,该函数通过电子邮件发送这些详细信息。成功后,弹出窗口显示给用户进行确认。

我已经应用了验证,并且我还能够运行ajax。但由于某种原因,我的成功函数一直返回0。请帮忙。

提交chrome上的开发工具输出:https://i.stack.imgur.com/PRsO1.jpg

在chrome上提交时的网络输出:https://i.stack.imgur.com/NQkIe.jpg

我的javascript

function contact_validations(){
event.preventDefault();
var name= jQuery('#contactname').val();
var cemail= jQuery('#contactemail').val();
var tel= jQuery('#contactphone').val();
var address= jQuery('#contactaddress').val();
var message= jQuery('#contactsubject').val();
var s=true;
if(name==''|| name==null ){
    jQuery('#name_err').html('Please enter your name');
    s=false;
    }else{  
        jQuery('#name_err').html('');
        }
if(cemail==''){
    jQuery('#email_err').html('Please enter email');
    s=false;
    }else{ 
        var x=cemail;
        var atpos=x.indexOf("@");
        var dotpos=x.lastIndexOf(".");
        if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
            { jQuery('#email_err').html("Please Enter  Valid E-mail Address"); s= false;} 
            else{
                jQuery('#email_err').html('');
                }
        }   

if(tel==''|| tel==null ){
    jQuery('#tel_err').html('Please enter your telephone number');
    s=false;
    }else{  
        jQuery('#tel_err').html('');
        }

if(address=='' || address==null){
    jQuery('#address_err').html('Please enter your address');
    s=false;
    }else{  
        jQuery('#address_err').html('');
        }

if(message=='' || message==null){
    jQuery('#message_err').html('Please enter your message');
    s=false;
    }else{  
        jQuery('#message_err').html('');
        }
        console.log( url );

if(s)
        {
            var url = "http://localhost:9080/c/wp-admin/admin-ajax.php";
            var data = {
                "action":"contact_email" ,
                "name": name ,
                "email": cemail ,
                "tel": tel ,
                "address": address ,
                "message": message 

            }
            jQuery.ajax({
            type: "POST",
            url: url,   
            contentType: 'JSON',
            data: JSON.stringify(data),
            beforeSend: function(){
                console.log('happened');
                jQuery('#ajax-loader').show();
                jQuery('#ajax-loader').html('<img src="http://localhost:9080/c/wp-content/themes/aussie/images/ajax-loader.gif">');
                },
            success: function(result) {
                jQuery('#ajax-loader').hide();
                console.log(result);

                if(result=='0')
                {
                    console.log("No Result")
                }
                if(result=='1')
                {   
                jQuery.fancybox({
                    href: '#contact-popup'
                });
                jQuery('#contactname').val('');
                jQuery('#contactemail').val('');
                jQuery('#contactphone').val('');
                jQuery('#contactaddress').val('');
                jQuery('#contactsubject').val('');
                console.log ( s );
              } 
            }
        });

        }
}

我的Javascript:

function contact_email() {

            $url = get_bloginfo('url');
            $storeemail = get_option('contactemailstored'); 
            $to = $storeemail['contactemail'];
            $subject = $url . ' - Contact';
            $name = $_POST['contactname'];
            $email = $_POST['contactemail'];
            $phone = $_POST['contactphone'];
            $address= $_POST['contactaddress'];
            $content= $_POST['contactsubject'];                                         
            $message = '<br>'.'Name:- ' .  $name . '<br>';
            $message  .= 'Email- ' . $email . '<br>';
            $message  .= 'Phone:- ' . $phone . '<br>';
            $message  .= 'Address  :' . $address  . '<br>';
            $message  .= 'Subject  :' . $content  . '<br>';

            $sent_message = wp_mail( $to , $subject, $message ); 
        if ( $sent_message ) {
            // the message was sent...
            echo '1';
        } else {
            // the message was not sent...
            echo '1';
        }
         exit;

    }
add_action('wp_ajax_contact_email', 'contact_email');
add_action('wp_ajax_nopriv_contact_email', 'contact_email');

问题是您正在将data转换为json,但wp希望一个名为action的字段能够找到正确的函数,因此您的返回值为0(未找到函数,未遇到出口,因此返回0)。

在这种情况下,您实际上不需要json值来发送表单,但它会期望json响应,因此在php函数中,您应该json_encode您的返回值。

jQuery(document).ready(function(){  
        var url = "<?php echo admin_url('admin-ajax.php'); ?>";
        var data = {
            "action": contact_email ,
            "name": name ,
            "email": cemail ,
            "tel": tel ,
            "address": address ,
            "message": message 
        }
        jQuery.ajax({
            type: "POST",
            url: url,   
            contentType: 'JSON',
            //JSON.stringify(data), -->will cause issue
            data: data,
            beforeSend: function(){
                console.log('happened');
            },
            success: function(result) {
                console.log(result);
            }
        });        
});

PHP

function contact_email() {
            // the message was sent...
      echo  json_encode(1); // if just sending 1, you can echo without json encode...
       exit;
}
add_action('wp_ajax_contact_email', 'contact_email');
add_action('wp_ajax_nopriv_contact_email', 'contact_email');

正在发送Json。。。

如果您愿意,可以将其他表单值转换为json,并包含在它自己的对象键中。例如

var data={};
data.action= 'hook';
data= JSON.stringify(formData);

等等。

非常感谢大家的帮助。我得到了很多帮助。虽然我发现了问题。问题是,对于wordpress,我不需要解析JSON中的数据。所以我用了普通类型,它起作用了。

此外,我的函数contact_email出现语法错误。。我使用

$name = $_POST['contactname'];
$email = $_POST['contactemail'];
$phone = $_POST['contactphone'];
$address= $_POST['contactaddress'];
$content= $_POST['contactsubject'];   

我用它的原始数据捕获表单数据,相反,它应该由AJAX中声明的val捕获。相反,我使用了这个,它起了作用:

$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['tel'];
$address= $_POST['address'];
$content= $_POST['message'];

这是更新后的代码:

Jquery:

function contact_validations(){
var name= jQuery('#contactname').val();
var cemail= jQuery('#contactemail').val();
var tel= jQuery('#contactphone').val();
var address= jQuery('#contactaddress').val();
var message= jQuery('#contactsubject').val();
var s=true;
if(name==''|| name==null ){
    jQuery('#name_err').html('Please enter your name');
    s=false;
    }else{  
        jQuery('#name_err').html('');
        }
if(cemail==''){
    jQuery('#email_err').html('Please enter email');
    s=false;
    }else{ 
        var x=cemail;
        var atpos=x.indexOf("@");
        var dotpos=x.lastIndexOf(".");
        if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
            { jQuery('#email_err').html("Please Enter  Valid E-mail Address"); s= false;} 
            else{
                jQuery('#email_err').html('');
                }
        }   

if(tel==''|| tel==null ){
    jQuery('#tel_err').html('Please enter your telephone number');
    s=false;
    }else{  
        jQuery('#tel_err').html('');
        }

if(address=='' || address==null){
    jQuery('#address_err').html('Please enter your address');
    s=false;
    }else{  
        jQuery('#address_err').html('');
        }

if(message=='' || message==null){
    jQuery('#message_err').html('Please enter your message');
    s=false;
    }else{  
        jQuery('#message_err').html('');
        }

if(s)
        {
            var url = "http://localhost:9080/c/wp-admin/admin-ajax.php";
            jQuery.ajax({
            type: "POST",
            url: url,   

            data:"action=contact_email&name="+name+"&email="+cemail+"&tel="+tel+"&address="+address+"&message="+message,
            beforeSend: function(){
                console.log('happened');
                jQuery('#ajax-loader').show();
                jQuery('#ajax-loader').html('<img src="http://localhost:9080/c/wp-content/themes/aussie/images/ajax-loader.gif">');
                },
            success: function(result) {
                jQuery('#ajax-loader').hide();
                console.log(result);

                if(result=='0')
                {
                    console.log("No Result")
                }
                if(result=='1')
                {   
                jQuery.fancybox({
                    href: '#contact-popup'
                });
                jQuery('#contactname').val('');
                jQuery('#contactemail').val('');
                jQuery('#contactphone').val('');
                jQuery('#contactaddress').val('');
                jQuery('#contactsubject').val('');
                console.log ( s );
              } 
            }
        });

        }
}

PHP

function contact_email() {

            $url = get_bloginfo('url');
            $storeemail = get_option('contactemailstored'); 
            $to = $storeemail['contactemail'];
            $subject = $url . ' - Contact';
            $name = $_POST['name'];
            $email = $_POST['email'];
            $phone = $_POST['tel'];
            $address= $_POST['address'];
            $content= $_POST['message'];                                            
            $message = '<br>'.'Name:- ' .  $name . '<br>';
            $message  .= 'Email- ' . $email . '<br>';
            $message  .= 'Phone:- ' . $phone . '<br>';
            $message  .= 'Address  :' . $address  . '<br>';
            $message  .= 'Subject  :' . $content  . '<br>';

            $sent_message = wp_mail( $to , $subject, $message ); 
        if ( $sent_message ) {
            // the message was sent...
            echo '1';
        } else {
            // the message was not sent...
            echo '0';
        }
         exit;

    }
add_action('wp_ajax_contact_email', 'contact_email');
add_action('wp_ajax_nopriv_contact_email', 'contact_email');
function contact_email() {
         //your code
         exit();
         die();
}

您需要在函数

的末尾包含这些PHP函数exit();die();