Home javascript Uncaught SyntaxError: Unexpected token & lt;

Uncaught SyntaxError: Unexpected token & lt;

Author

Date

Category

I am using a code that successfully works on 3 sites (sending an application from the form to an e-mail and displaying a thank you window).

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

Form code and JS:

& lt; div id = "modal" class = "modal" & gt;
  & lt; div class = "text" align = "center" & gt;
      & lt; form id = "mailer_form" action = "mailer.php" method = "post" class = "form1" & gt;
            & lt; h3 & gt; Leave a request and & lt; br & gt; get a quote & lt; / h3 & gt;
            & lt; input type = "text" name = "name" class = "inputbox" placeholder = "Your name:" required & gt;
            & lt; input type = "text" name = "phone" class = "inputbox" placeholder = "Your phone:" required & gt;
            & lt; input type = "submit" name = "submit" class = "button" value = "Order flowers" & gt;
             & lt; a href = "# close" title = "Close" & gt; & lt; / a & gt;
      & lt; / form & gt;
  & lt; / div & gt;
& lt; / div & gt;

& lt; script & gt;
// 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 & amp; & amp; parseInt (data_result.error) === 0) {
var html_result = "& lt; h3 & gt; Thank you for your request! & lt; br & gt; & lt; br & gt; We will contact you shortly! & lt; / h3 & gt; & lt; img src = 'img / up.png' style = 'margin-top: 35px;' & gt; "
$ ('div # modal div.text'). html (html_result);
// alert ("OK");
} else if (parseInt (data_result.success) === 0 & amp; & amp; 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.
});
& lt; / script & gt; 

Answer 1, authority 100%

The second condition in the success function does not work for you, namely:

if (parseInt (data_result.success) === 1 & amp; & amp; parseInt (data_result.error) == = 0) {
  var html_result = "& lt; h3 & gt; Thank you for your request! & lt; br & gt; & lt; br & gt; We will contact you shortly! & lt; / h3 & gt; & lt; img src = 'img / up.png' style = 'margin-top: 35px;' & gt; "
  $ ('div # modal div.text'). html (html_result);
  // alert ("OK");
}

If the conditions in the JSON response are parseInt (data_result.success) === 1 and parseInt (data_result.error) === 0 definitely works, then put “; ” after the line

var html_result = "& lt; h3 & gt; Thank you for your request! & lt; br & gt; & lt; br & gt; We will contact & Lt; br & gt; coming soon! & Lt; / h3 & gt; & lt; img src = 'img / up.png' style = 'margin-top: 35px;' & gt; "

Should work.


Answer 2

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

console.log ('RESPONDE = & gt;' + data);

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

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions