我在提交表单时尝试发出AJAX请求,但我甚至无法在“网络”面板中看到该请求,因为在点击URL 之前失败了。这是代码:
// contact form
$(function() {
$(document).on("submit", "#contact-form", function(e) {
// prevents normal submit
e.preventDefault();
var form = $(this);
var url = form.attr("action");
$.post(url, function(data) {
alert(data);
})
.fail(function (jqXHR, textStatus, errorThrown) {
alert(errorThrown); // TypeError: Cannot read property 'count' of undefined
});
});
})
这是HTML:
这是HTML:
<form name="contact" method="post" action="/app_dev.php/contacts/submit" id="contact-form">
<div class="row">
<div class="col-xl-6 form-group">
<input type="text" id="contact_firstname" name="contact[firstname]" maxlength="255" class="form-control form-control-lg" placeholder="Entre ton nom*" />
</div>
<div class="col-xl-6 form-group">
<input type="text" id="contact_lastname" name="contact[lastname]" required="required" maxlength="255" class="form-control form-control-lg" placeholder="Entre ton nom de famille*" />
</div>
<div class="col-xl-6 form-group">
<input type="email" id="contact_email" name="contact[email]" required="required" maxlength="255" class="form-control form-control-lg" placeholder="Entre ton adresse e-mail*" />
</div>
<div class="col-xl-6 form-group">
<input type="text" id="contact_subject" name="contact[subject]" required="required" maxlength="255" class="form-control form-control-lg" placeholder="Entre le sujet*" />
</div>
<div class="col-12 mb-5">
<textarea id="contact_message" name="contact[message]" required="required" class="form-control" rows="3" placeholder="Écris ton message*"></textarea>
</div>
<input type="hidden" name="_csrf_token" value="Su1QsqB8LZdqqVRxrxBPeXQNpp29QlYEt7yvg13hzCI">
<div class="col-xl-3">
<button type="submit" class="btn btn-xl btn-block">Envoyer</button>
</div>
</div>
</form>
我始终收到此消息的提醒:TypeError: Cannot read property 'count' of undefined
。
如果我尝试将相同的代码放在提交之外(在文档就绪事件中),可以正常工作:
// contact form
$(function() {
//$(document).on("submit", "#contact-form", function(e) {
//// prevents normal submit
//e.preventDefault();
//});
var form = $("contact-form");
var url = form.attr("action");
$.post(url, function(data) {
alert(data);
})
.fail(function (jqXHR, textStatus, errorThrown) {
alert(errorThrown); // WORKS FINE
});
})
这些是我的JavaScript文件:
<script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
<script type="text/javascript" src="/js/app.js"></script>
答案 0 :(得分:0)
我认为这是由 Symfony Web Profiler工具栏引起的一个非常奇怪的错误!幸运的是,修复工作已于13小时前发布:
https://github.com/symfony/symfony/releases/tag/v2.8.36
这就是问题所在: