Php Wordpress和AJAX-从数组输出

Php Wordpress和AJAX-从数组输出,php,json,ajax,wordpress,output,Php,Json,Ajax,Wordpress,Output,通过一个插件,我添加了一个在页面上放置表单的短代码。在服务器上,我有以下功能: function ajax_calc(){ // with the POST if(isset($_POST['action']) && $_POST['action'] == 'ajax_calc'){ //clearing and adapting input string $width = s

通过一个插件,我添加了一个在页面上放置表单的短代码。在服务器上,我有以下功能:

        function ajax_calc(){
    // with the POST  

            if(isset($_POST['action']) && $_POST['action'] == 'ajax_calc'){
    //clearing and adapting input string
                $width      = str_replace(',','.',trim(htmlspecialchars($_POST['width'])));    

                $length     = str_replace(',','.',trim(htmlspecialchars($_POST['length'])));  

                $height     = str_replace(',','.',trim(htmlspecialchars($_POST['height'])));

                $m_rul      = str_replace(',','.',trim(htmlspecialchars($_POST['m_rul'])));     

                $k_zap      = str_replace(',','.',trim(htmlspecialchars($_POST['k_zap'])));

    // an error if not a float number 
                if(!(float)$width || $width ==''){     
                    echo implode(array('loadmsgerr'=>'Error! Width should be number only!'));

                }
                elseif(!(float)$length || $length ==''){
                    echo implode(array('loadmsgerr'=>'Error! Lenght should be number only!'));

                }
                elseif(!(float)$height || $height ==''){
                    echo implode(array('loadmsgerr'=>'Error! Height should be number only!'));

                }
                else{
    // return result     
                    $Perimeter = ($width + $length) * 2 ; // 
                    $Square =  $Perimeter * $height;      //                                    // there will be other calculations...

                    $it_test = 10*$k_zap + $m_rul;

//     here we have an ARRAY with data
                    $answers = array('S' => $Square, 'P' => $Perimeter, 'it_test' => $it_test);                      
                    echo json_encode($answers);
                            }
                    }
            }
接下来,我有一个JS:

jQuery(document).ready(function(){
// catch a click 
    jQuery('#calc #form_calc_id').on('submit', function(e){
        jQuery('#calc .result').show().text(ajax_calc_object.loadingmessage);//Обработка...
//AJAX with POST on url. DataType is json for array        
        jQuery.ajax({
            type: 'POST',
            dataType: 'json',
            url: ajax_calc_object.ajaxurl,
            data: { //data from our form
                'action': 'ajax_calc', //
                'width': jQuery('#calc #width').val(), 
                'length':jQuery('#calc #length').val(),
                'height': jQuery('#calc #height').val(),                            
//                'security': jQuery('#security').val()
                'm_rul': jQuery('#pa_м-в-рулоне').text(),
                'k_zap': jQuery('#pa_k-zap').text(),

            },
            success: function(data){
                var resperr = data.loadmsgerr;

                if(!resperr){//if its all clear
                    json.parse(data);
                    jQuery('#calc .result').html(data.S + data.P); //put the answer to .result taget div

                }
                else{
                    jQuery('#calc .result').text(resperr); //or an error message
                }

            },
            error: function(xhr, textStatus, errorThrown) {//console log
                if (xhr.status != 0) {
                    var msg = ' (' + xhr.status + ') ';
                    if (textStatus) msg += ': ' + textStatus;
                    if (xhr.status < 200) {
                        msg = 'AJAX Informational ' + msg;
                    } else if (xhr.status < 300) {
                        msg = 'AJAX Success ' + msg;
                    } else if (xhr.status < 400) {
                        msg = 'AJAX Redirection ' + msg;
                    } else if (xhr.status < 500) {
                        msg = 'AJAX Client Error' + msg;
                    } else {
                        msg = 'AJAX Server Error' + msg;
                    }
                    console.log(msg);
                } else {
                    console.log(errorThrown);
                }
            }
        });
        e.preventDefault();
    });
});
jQuery(文档).ready(函数(){
//点击
jQuery('#calc#form#calc_id')。关于('submit',函数(e){
jQuery('#calc.result').show().text(ajax#calc#object.loadingmessage);//。。。
//url上带有POST的AJAX。数组的数据类型是json
jQuery.ajax({
键入:“POST”,
数据类型:“json”,
url:ajax\u calc\u object.ajaxurl,
数据:{//来自表单的数据
“操作”:“ajax_calc”//
“宽度”:jQuery(“#计算#宽度”).val(),
“length”:jQuery(“#计算#长度”).val(),
“height”:jQuery(“#计算#高度”).val(),
//“安全性”:jQuery(“#安全性”).val()
'm#rul':jQuery('#pa_uuМ-ö-С-Саааа)。text(),
'k#u zap':jQuery('pa#u k-zap').text(),
},
成功:功能(数据){
var resperr=data.loadmsgerr;
如果(!resperr){//如果全部清除
json.parse(数据);
jQuery('#calc.result').html(data.S+data.P);//将答案放在.result taget div中
}
否则{
jQuery('#calc.result').text(resperr);//或错误消息
}
},
错误:函数(xhr、textStatus、errorshown){//控制台日志
如果(xhr.status!=0){
var msg='('+xhr.status+');
如果(textStatus)消息+=':'+textStatus;
如果(xhr.status<200){
msg='AJAX information'+msg;
}否则如果(xhr.status<300){
msg='AJAX Success'+msg;
}否则如果(xhr.status<400){
msg='AJAX重定向'+msg;
}否则如果(xhr.status<500){
msg='AJAX客户端错误'+msg;
}否则{
msg='AJAX服务器错误'+msg;
}
控制台日志(msg);
}否则{
console.log(错误抛出);
}
}
});
e、 预防默认值();
});
});
问题是我的页面上没有结果。 控制台说:

AJAX成功(200):解析器错误

从我的职能来看:

{“S”:“p”:“it_测试”:“19”}0


我认为这都是从json字符串末尾的0开始的。但是我不明白为什么它会出现在那里

如果我们将
wp_die()
放在ajax_calc()函数的末尾,0将不会出现。

您也可以发布您的php代码吗?函数位于WordPress插件文件中,它太大,无法在此处读取。。此外,现在我有了答案)