2016-02-29 37 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不會出現。