阿贾克斯成功返回0 [英] ajax success returning 0

查看:105
本文介绍了阿贾克斯成功返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个小的帮助。我设计我的自定义的AJAX功能。这是一个简单的形式输入5个值对它们进行验证,然后通过AJAX将数据发送到一个PHP函数和函数邮件这些细节。成功后弹出显示到确认的用户。

我已经申请了验证,我也能够运行AJAX。但由于某些原因,我的成功,功能不断返回0。请帮忙。

开发工具上提交镀铬的输出: http://imgur.com/m5Ah0md

网络上提交关于铬的输出: http://imgur.com/R7q9WnH

我的JavaScript

 函数contact_validations(){
。事件preventDefault();
VAR名称=的jQuery('#联系人姓名)VAL()。
VAR cemail =的jQuery('#contactemail)VAL()。
VAR电话=的jQuery('#contactphone)VAL()。
。VAR地址=的jQuery('#contactaddress)VAL();
VAR消息=的jQuery('#contactsubject)VAL()。
VAR S =真实;

如果(名称==''||名== NULL){
    jQuery的('#name_err)HTML(请输入您的姓名)。
    S = FALSE;
    }其他{
        jQuery的('#name_err)HTML('')。
        }

如果(cemail ==''){
    jQuery的('#email_err)HTML(请输入电子邮件')。
    S = FALSE;
    }其他{
        VAR X = cemail;
        VAR atpos = x.indexOf(@);
        变种dotpos = x.lastIndexOf(。);
        如果(atpos&小于1 || dotpos&所述; atpos +2 || dotpos +2; = x.length)
            {jQuery的('#email_err)HTML(请输入有效的电子邮件地址); S = FALSE;}
            其他{
                jQuery的('#email_err)HTML('')。
                }
        }


如果(电话:==''||电话== NULL){
    。jQuery的('#tel_err)HTML(请输入您的电话号码);
    S = FALSE;
    }其他{
        jQuery的('#tel_err)HTML('')。
        }



如果(地址==''||地址== NULL){
    jQuery的('#address_err)HTML(请输入您的地址)。
    S = FALSE;
    }其他{
        jQuery的('#address_err)HTML('')。
        }


如果(消息==''||消息== NULL){
    jQuery的('#message_err)HTML(请输入您的信息)。
    S = FALSE;
    }其他{
        jQuery的('#message_err)HTML('')。
        }
        执行console.log(URL);


如果(多个)
        {
            VAR URL =HTTP://本地主机:9080 / C /可湿性粉剂管理员/管理员-ajax.php;
            VAR数据= {

                行动:contact_email
                名的名称,
                电子邮件:cemail,
                电话:电话,
                地址:地址,
                消息:消息


            }
            jQuery.ajax({
            键入:POST,
            网址:网址,
            的contentType:JSON,
            数据:JSON.stringify(数据),
            beforeSend:函数(){
                的console.log('发生');
                。jQuery的('#阿贾克斯装载机)显示();
                jQuery的('#阿贾克斯装载机)HTML('< IMG SRC =HTTP://本地主机:9080 / C /可湿性粉剂内容/主题/澳元/图片/ AJAX-loader.gif>')。
                },
            成功:函数(结果){
                。jQuery的('#阿贾克斯装载机)隐藏();
                执行console.log(结果);


                如果(结果== 0)
                {
                    的console.log(无结果)
                }
                如果(结果=='1')
                {
                jQuery.fancybox({
                    HREF:#接触,弹出
                });
                jQuery的('#联系人姓名)VAL('')。
                jQuery的('#contactemail)VAL('')。
                jQuery的('#contactphone)VAL('')。
                jQuery的('#contactaddress)VAL('')。
                jQuery的('#contactsubject)VAL('')。

                执行console.log(S);
              }
            }
        });


        }
}
 

我的javascript:

 函数contact_email(){



            $ URL = get_bloginfo('链接');
            $ storeemail = get_option('contactemailstored')​​;
            $到= $ storeemail ['contactemail'];
            $主题= $网址。 ' - 联系';
            $ NAME = $ _ POST ['联系人姓名'];
            $电子邮件= $ _ POST ['contactemail'];
            $手机= $ _ POST ['contactphone'];
            $地址= $ _ POST ['contactaddress'];
            $内容= $ _ POST ['contactsubject'];
            $消息='< BR>''名称: - 。 $名字。 '< BR>';
            $消息。='的电子邮件 - '。 $电子邮件。 '< BR>';
            。$消息='电话: - 。 $手机。 '< BR>';
            $消息='地址:。 $地址。 '< BR>';
            $消息='主题:。 $内容。 '< BR>';


            $ sent_message = wp_mail($到,$主题,$消息);

        如果($ sent_message){
            //发送消息...
            回声'1';
        } 其他 {
            //该消息未发送...
            回声'1';
        }

         出口;


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

add_action('wp_ajax_nopriv_contact_email','contact_email');
 

解决方案

问题是要转换数据来JSON,但WP期望一个名为领域动作找到正确的功能,因此您的返回值为0(功能未找到,没有出口遇到这么0返回)。

在这种情况下,你其实并不需要的JSON值发送的形式,但它会期待一个JSON响应,以便在你的PHP函数,你应该 json_en code 您返回值。

  jQuery的(文件)。就绪(函数(){

        VAR URL =< PHP的回声ADMIN_URL(管理-ajax.php');?>中;
        VAR数据= {

            行动:contact_email,
            名的名称,
            电子邮件:cemail,
            电话:电话,
            地址:地址,
            消息:消息
        }

        jQuery.ajax({
            键入:POST,
            网址:网址,
            的contentType:JSON,
            //JSON.stringify(data), - >会导致问题
            数据:数据,
            beforeSend:函数(){
                的console.log('发生');
            },
            成功:函数(结果){
                执行console.log(结果);
            }
        });
});
 

PHP

 函数contact_email(){

            //发送消息...
      回声json_en code(1); //如果只是送1,你可以不呼应JSON EN code ...
       出口;

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

add_action('wp_ajax_nopriv_contact_email','contact_email');
 

发送的Json ...

如果你愿意,你可以转换成其他形式的值,JSON和在其自己的对象的关键。例如,

  VAR数据= {};
data.action ='钩子';
数据= JSON.stringify(FORMDATA);
 

等等。

I need a small help. I have designed my custom ajax function. It is a simple form which inputs 5 values validates them and then sends data through ajax to a php function and the function emails those details. Upon success a popup is shown to the user of confirmation.

I have applied the validation and i am also able to run ajax. But for some reason my success function keeps on returning 0. Please help.

Dev tools output on submit chrome: http://imgur.com/m5Ah0md

Network output on submit on chrome: http://imgur.com/R7q9WnH

My 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 );
              } 
            }
        });


        }
}

My 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');

解决方案

The issue is you are converting data to json but wp expects a field called action to find the correct function, hence your return value is 0 (function is not found, no exit encountered so 0 returned).

In this instance you actually do not need json values send the form, but it will expect a json response so in your php function you should json_encode your return value.

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');

Sending Json...

If you wish you can convert the other form values to json and include in its own object key. e.g.

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

And so on.

这篇关于阿贾克斯成功返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆