2016-02-29 41 views
0

使用插件我添加了一个将页面上的表单加入的简码。在服务器上我有这样的功能:Wordpress和AJAX - 数组输出

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

麻烦的是,我有我的网页上没有任何结果。 控制台说:

AJAX成功(200):parsererror

从我的功能我看到这一点:

{ “S”: “”, “P”: “” ,“it_test”:“19”} 0

我觉得它的全部从0开始在json字符串的末尾。但我不知道为什么它出现在那里..

+0

你可以发布你的php代码吗? – Mark

+0

函数是在WordPress插件文件中,它太大以至于无法在这里阅读..此外,现在我有答案) –

回答

0

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