2017-07-08 2 views
0

Wie ich sehen kann, gibt es keine Fehler im Code, aber immer noch zeigt dies SyntaxError: fehlt; vor AnweisungscreenshotMailchimp ajax SyntaxError bei der Formularübergabe

Hier ist die Form:

<form id="bildo_mc_form" action="https://stylishcreativity.us3.list-manage.com/subscribe/post-json?u=7c7040d58ca368b8f8063c1ea&amp;id=f67dfc67a4" method="post"> 
    <input id="mc-email" type="email" name="EMAIL" placeholder="Your Email"> 
    <label for="mc-email"></label> 
    <button type="submit">Send</button> 
</form> 

Hier ist die Js:

jQuery("#bildo_mc_form").submit(function(e){ 

    console.log("form submitted"); 

    function callbackFunction(data){ 
     window.location.href = "http://thanks.com"; 
     console.log("sucess result" + data.result); 
     console.log("sucess msg" + data.msg); 
    } 

    var url = jQuery(this).prop('action'); // the script where you handle the form input. 
    jQuery.ajax({ 
      type: "GET", 
      url: url, 
      data: jQuery("#bildo_mc_form").serialize(), // serializes the form's elements. 
      dataType: "jsonp", 
      contentType: "application/json; charset=utf-8", 
      error: function(error){ 
       // According to jquery docs, this is never called for cross-domain JSONP requests 
       console.log("Error Result" + error.result); 
      }, 
      success: callbackFunction 
     }); 

    e.preventDefault(); // avoid to execute the actual submit of the form. 
}); 

Wie kann ich dieses Problem beheben? Pls Hilfe :)

+0

ich Ihren Code in jshint validiert haben, gibt es keine Probleme. Haben Sie [diese ähnliche Frage] überprüft (https://stackoverflow.com/questions/19456146/ajax-call-and-clean-json-but-syntax-error-missing-before-statement)? Dies könnte das gleiche Problem sein. –

+0

Ich habe bereits alle möglichen Korrekturen ausprobiert, die sie auf der ähnlichen Frage erwähnt haben, funktioniert immer noch nicht! Für MailChimp Formular müssen Sie DataType auf JSONP oder das Formular wird nicht http://prntscr.com/ftf6gj –

Antwort

0

Da Sie JSON von Ajax erhalten, sollten Sie dataType: "json" setzen.

Nicht jsonp.

jsonp Antwort wird als eine Funktion ausgeführt und verursacht den Fehler.

jQuery("#bildo_mc_form").submit(function(e){ 
 

 
    console.log("form submitted"); 
 

 
    function callbackFunction(data){ 
 
     window.location.href = "http://thanks.com"; 
 
     console.log("sucess result" + data.result); 
 
     console.log("sucess msg" + data.msg); 
 
    } 
 

 
    var url = jQuery(this).prop('action'); // the script where you handle the form input. 
 
    jQuery.ajax({ 
 
      type: "GET", 
 
      url: url, 
 
      data: jQuery("#bildo_mc_form").serialize(), // serializes the form's elements. 
 
      dataType: "json", 
 
      contentType: "application/json; charset=utf-8", 
 
      error: function(error){ 
 
       // According to jquery docs, this is never called for cross-domain JSONP requests 
 
       console.log("Error Result" + error.result); 
 
      }, 
 
      success: callbackFunction 
 
     }); 
 

 
    e.preventDefault(); // avoid to execute the actual submit of the form. 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form id="bildo_mc_form" action="https://stylishcreativity.us3.list-manage.com/subscribe/post-json?u=7c7040d58ca368b8f8063c1ea&amp;id=f67dfc67a4" method="post"> 
 
    <input id="mc-email" type="email" name="EMAIL" placeholder="Your Email"> 
 
    <label for="mc-email"></label> 
 
    <button type="submit">Send</button> 
 
</form>

+0

setzen, wenn ich den Datentyp auf JSON setzen, wird es einen anderen Fehler zeigen 'Cross-Origin-Anfrage blockiert: Die Die Richtlinie "Gleiche Herkunft" verbietet das Lesen der Remote-Ressource unter https://stylishcreativity.us3.list-manage.com/subscribe/post-json?u=7c7040d58ca368b8f8063c1ea&id=f67dfc67a4&EMAIL=asashfasf%40gmail.com. (Grund: CORS-Header 'Access-Control-Allow-Origin' fehlt) .' http://prntscr.com/ftf6gj –

+0

@Mashiur Rahman, Es ist eine andere Frage, wie 'Cross-Origin'-Anfragen zu erledigen sind. Grundsätzlich sollte der Host "https: // styliccreativity.us3.list-manage.com" einen gültigen Antwortkopf "Access-Control-Allow-Origin" senden. Die Art der Einrichtung hängt vom verwendeten Webserver ab. –

Verwandte Themen