简体   繁体   English

Ajax请求不处理POST变量

[英]Ajax Request not processing POST Variables

I have read every post and have tried everything I can for the last few hours - so I have turned to the community for help! 我已经阅读过每一篇文章,并且在过去的几个小时里尝试了所有的东西 - 所以我向社区寻求帮助!

I have a BMI calculator, which works fine. 我有一个BMI计算器,工作正常。 But I am trying to process the form through AJAX, now my ajax request works fine and I am recieving a response, but it does not contain any of my POST data. 但我试图通过AJAX处理表单,现在我的ajax请求工作正常,我收到了响应,但它不包含任何我的POST数据。

Also let me mention that I am working with WordPress. 还让我提一下我正在使用WordPress。

Here is my AJAX: 这是我的AJAX:

jQuery(document).ready(function($) {

    /*--------------------------------------------------------------
     # BMI Calculator Ajax Call
     --------------------------------------------------------------*/
    $('form#bmi-calculator').on( 'submit', function(e) {

        // ajax request
        $.ajax({
            url : ajax_object.ajax_url,
            type : 'POST',
            data : {
                'action' : 'fitnessgym_bmi_calculator',
            },
            success : function( response ) {
                $('.bmi-results').append( response );
                console.log( response );
            },
            alert : function( error ) {
                alert( 'error' )
            }

        });
        e.preventDefault();
    });

});

And here is my callback function: 这是我的回调函数:

/*-------------------------------------------------------------------
  Process the BMI Calculator
--------------------------------------------------------------------*/
add_action( 'wp_ajax_fitnessgym_bmi_calculator', 'fitnessgym_bmi_calculator' );
add_action( 'wp_ajax_nopriv_fitnessgym_bmi_calculator', 'fitnessgym_bmi_calculator' );


function fitnessgym_bmi_calculator() {

    // Sanitize post variables.
    $allowed_html = array();
    $weight = trim(sanitize_text_field(wp_kses($_POST['bmi_weight'], $allowed_html)));
    $height = trim(sanitize_text_field(wp_kses($_POST['bmi_height'], $allowed_html)));
    $age = trim(sanitize_text_field(wp_kses($_POST['bmi_age'], $allowed_html)));

    // Do the Calculation.
    $equation = $weight / $height;

    echo 'Weight is: ' . $weight . '<br>';
    echo 'Height is: ' . $height . '<br>';


    // If BMI is Healthy or Not.
    if ($equation < 18.5) {
        $health = esc_html__('Underweight', 'fitnessgym');
    } elseif ($equation > 18.5 && $equation < 24.9) {
        $health = esc_html__('Healthy', 'fitnessgym');
    } elseif ($equation > 25 && $equation < 29.9) {
        $health = esc_html__('Overweight', 'fitnessgym');
    } elseif ($equation > 30) {
        $health = esc_html__('Obese', 'fitnessgym');
    }

    $bmi_response = 'Your BMI is:' . number_format(esc_html($equation), 1) . '<br>Your health is: ' . $health;

    echo $bmi_response;

    wp_die();

}

Basically, when I submit the form what returns is: 基本上,当我提交表单时返回的是:

Weight is: 
Height is: 
Your BMI is:
Your health is: Underweight

So without any of the POST information. 所以没有任何POST信息。 Also let me mention that I have tried the POST variables without any sanitizing (encase anyone recommends that) :) 另外让我提一下,我已经尝试过POST变量而没有任何消毒(包括任何人建议):)

I can not figure out for the life of me, why this will not display. 我无法弄清楚我的生活,为什么这不会显示出来。 Any help is appreciated, thank-you. 感谢任何帮助,谢谢。

You're not sending the data you are expecting on the backend. 您没有在后端发送您期望的数据。

Look again at your ajax call 再看看你的ajax电话

$.ajax({
    ...
    data : {
        'action': 'fitnessgym_bmi_calculator',
    },
    ...
});

Add the data you are expecting to get is 添加您期望获得的数据

{
    'action': 'fitnessgym_bmi_calculator',
    'bmi_weight': 0,
    'bmi_height': 0.
    'bmi_age': 0
}

Add those missing key-value pairs to data and everything should work. 将那些缺少的键值对添加到data ,一切都应该有效。

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

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