With the help of a plugin I add a shortcode to WordPress, which inserts a form for calculating on a page. The server part of the form is such a function:

function ajax_calc(){ // ΠŸΡ€ΠΈΠ΅ΠΌ ΠΈ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° POST запроса if(isset($_POST['action']) && $_POST['action'] == 'ajax_calc'){ //ΡˆΠΈΡ€ΠΈΠ½Π°, экранируСм всС лишнСС, ΡƒΠ±ΠΈΡ€Π°Π΅ΠΌ ΠΏΡ€ΠΎΠ±Π΅Π»Ρ‹ ΠΈ замСняСм "," Π½Π° ".", Ρ‚.ΠΊ. ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒ ΠΌΠΎΠΆΠ΅Ρ‚ ввСсти любой ΠΈΠ· Π·Π½Π°ΠΊΠΎΠ² $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']))); // Π²ΠΎΠ·Π²Ρ€Π°Ρ‚ΠΈΠΌ ΠΎΡˆΠΈΠ±ΠΊΡƒ, Ссли ΠΏΡ€ΠΈΡˆΠ΅Π΄ΡˆΠΈΠΉ ΠΊ Π½Π°ΠΌ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ Π½Π΅ число 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{ // ΠΈΠ½Π°Ρ‡Π΅ Π²Π΅Ρ€Π½Π΅ΠΌ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ $Perimeter = ($width + $length) * 2 ; //ΠΏΠ΅Ρ€ΠΈΠΌΠ΅Ρ‚Ρ€ $Square = $Perimeter * $height; //ΠΏΠ»ΠΎΡ‰Π°Π΄ΡŒ Π±ΠΎΠΊΠΎΠ²Ρ‹Ρ… повСрхностСй // Π΄Ρ€ΡƒΠ³ΠΈΠ΅ вычислСния, прСдобразования... // Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹ массив с Π΄Π°Π½Π½Ρ‹ΠΌΠΈ $it_test = 10*$k_zap + $m_rul; $answers = array('S' => $Square, 'P' => $Perimeter, 'it_test' => $it_test); echo json_encode($answers); } } } 

And JS with this:

 jQuery(document).ready(function(){ // Π»ΠΎΠ²ΠΈΠΌ ΠΊΠ»ΠΈΠΊ ΠΏΠΎ ΠΊΠ½ΠΎΠΏΠΊΠ΅ ΠΊΠ°Π»ΡŒΠΊΡƒΠ»ΡΡ‚ΠΎΡ€Π° jQuery('#calc #form_calc_id').on('submit', function(e){ jQuery('#calc .result').show().text(ajax_calc_object.loadingmessage);//ΠžΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠ°... //AJAX запрос ΠΌΠ΅Ρ‚ΠΎΠ΄ΠΎΠΌ POST Π½Π° ΡƒΠΊΠ°Π·Π°Π½Π½Ρ‹ΠΉ url. Π’ΠΈΠΏ Π΄Π°Π½Π½Ρ‹Ρ… опрСдСляСм ΠΊΠ°ΠΊ json jQuery.ajax({ type: 'POST', dataType: 'json', url: ajax_calc_object.ajaxurl, data: { //ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π΅ΠΌ Π΄Π°Π½Π½Ρ‹Π΅ ΠΈΠ· Ρ„ΠΎΡ€ΠΌΡ‹ Π² шаблонС '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){//Ссли Π½Π΅Ρ‚ ошибок json.parse(data); jQuery('#calc .result').html(data.S + data.P); //вставляСм Π² Ρ‚Π΅Π³ с классом .result ΠΏΠΎΠ»ΡƒΡ‡Π΅Π½Π½Ρ‹ΠΉ ΠΎΡ‚Π²Π΅Ρ‚ jQuery('#Обои').val(); } else{ jQuery('#calc .result').text(resperr); //ΠΈΠ»ΠΈ Ρ‚ΡƒΠ΄Π° ΠΆΠ΅ Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌ ΠΎΡˆΠΈΠ±ΠΊΡƒ } }, error: function(xhr, textStatus, errorThrown) {//ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΈ Π²Ρ‹Π²ΠΎΠ΄ Π² консоль ошибок 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(); }); }); 

The problem is that all this does not display anything on the page. An error is written to the console.

AJAX Success (200): parsererror

And in the data transferred from the function, I see the line

{"S": "", "P": "", "it_test": "19"} 0

So, I suspect that the whole problem is exactly this 0 at the end of the json line. But where does he come from, I don’t understand at all. Maybe someone familiar with the mechanism?

    1 answer 1

    To avoid getting zero at the end of the ajax response, at the very end of the ajax_calc() function, call wp_die()

    • So simple? It really worked! Thank you) - Sergey Vdovin