I use the code that successfully works on 3 sites (sending the application form from the e-mail and the withdrawal of the window of thanks).

When I try to launch it on a new site, an error appears in the Chrome console (and in other browsers): http://prntscr.com/8n0rs4 (screenshot).

Form Code and JS:

<div id="modal" class="modal"> <div class="text" align="center"> <form id="mailer_form" action="mailer.php" method="post" class="form1"> <h3>Оставьте заявку и<br>получите расчет стоимости</h3> <input type="text" name="name" class="inputbox" placeholder="Ваше имя:" required> <input type="text" name="phone" class="inputbox" placeholder="Ваш телефон:" required> <input type="submit" name="submit" class="button" value="Заказать цветы"> <a href="#close" title="Закрыть"></a> </form> </div> </div> 

  <script> //callback handler for form submit $("#mailer_form").submit(function(e) { var postData = $(this).serializeArray(); var formURL = $(this).attr("action"); $.ajax( { url : formURL, type: "POST", data : postData, success:function(data, textStatus, jqXHR) { data_result = JSON.parse(data); if(typeof data_result === 'undefined') { // alert('Problem with returned JSON'); }else{ if(parseInt(data_result.success) === 1 && parseInt(data_result.error) === 0) { var html_result="<h3>Спасибо за вашу заявку!<br><br>Мы свяжемся с Вами<br>в ближайшее время!</h3><img src='img/up.png' style='margin-top: 35px;'>" $('div#modal div.text').html(html_result); //alert("OK"); }else if(parseInt(data_result.success) === 0 && parseInt(data_result.error) === 1) { // alert(data_result.msg); } } }, error: function(jqXHR, textStatus, errorThrown) { // alert("Error!"); console.log(data); } }); e.preventDefault(); //STOP default action // e.unbind(); //unbind. to stop multiple form submit. }); </script> 

I repeat that this code works fine on other sites. Copied 1 to 1, the same scripts are connected.

What could be the problem?

  • PS The form is triggered and the application for mail leaves. But the information in the <h1> </ h1> tag does not change - in theory, the inscription should be replaced. - Alexander Tymoshchuk
  • What does json look like? - u_mulder
  • Did not quite understand the question :) I am new to JS) - Alexander Timoshchuk
  • Himself faced with such a problem. I decided to look at the similar code of other specialists. More closely to punctuation. I stood in the created object; although it is correct simply, because of this, all the fuss is boron - Paul

2 answers 2

You do not have the second condition in the function of success, namely:

 if(parseInt(data_result.success) === 1 && parseInt(data_result.error) === 0) { var html_result="<h3>Спасибо за вашу заявку!<br><br>Мы свяжемся с Вами<br>в ближайшее время!</h3><img src='img/up.png' style='margin-top: 35px;'>" $('div#modal div.text').html(html_result); //alert("OK"); } 

If in the JSON response the conditions parseInt(data_result.success) === 1 and parseInt(data_result.error) === 0 exactly work, then put " ; " after the line

 var html_result="<h3>Спасибо за вашу заявку!<br><br>Мы свяжемся с Вами<br>в ближайшее время!</h3><img src='img/up.png' style='margin-top: 35px;'>" 

It should work.

  • Specially checked this moment on other sites (with the same code) - there is no ";" and it works. Put ";" at my place now, the same mistake anyway :( - Alexander Timoshchuk
  • Then in the FireBag in Mozilla FF, copy the response code from the console and discard it. - mix
  • 1. This site uses the certificate SHA-1; It is recommended to use certificates with signature algorithms that use stronger hash functions than SHA-1. 2. SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data PS When you click on the second error, it indicates an error here: data_result = JSON.parse (data); - Alexander Tymoshchuk
  • Then try the line data_result = JSON.parse(data); replace with data_result = data; But you still have not copied. Need a JSON response, not an error. - mix
  • With data_result = data; There is no error, but the form does not change the value in the <h1> tag. About "I need a JSON response, not errors." - I apologize wildly for the nubism, but tell me, please, how to look at it? - Alexander Tymoshchuk

just add before data_result = JSON.parse(data); this

 console.log('RESPONDE=>'+data); 

then reload the page, right-click and select the developer tools, perform the action and see the result obtained. Most likely you have received incorrect JSON or have none at all.

  • Not after, but before. If JSON.parse falls, then there will be no one left. - Qwertiy
  • Yes, quite right - Roman Zykov