[英]Why do I get a “Post” 404 error when submitting an ajax contact form in WP
I'm in the process of building an ajax contact-form for a WordPress theme and am running into an issue when submitting the form; 我正在为WordPress主题构建ajax联系人表单,并且在提交表单时遇到问题; the console keeps saying it can't retrieve my mail-form.php file (used to format the email) when I'm pretty sure I've got the url right.
当我确定我的网址正确无误时,控制台一直说它无法检索我的mail-form.php文件(用于格式化电子邮件)。
Here's the code that I'm using: 这是我正在使用的代码:
jQuery.ajax({
type: "POST",
url: "<?php echo get_template_directory_uri(); ?>/mail-form.php",
cache: false,
data: "name=" + name + "&email=" + email + "&message=" + message,
success: function (html) {
jQuery("#contact-form").slideUp("slow");
jQuery("#contact-form").after("<p><span class='center' id='send-message'>Your message has been sent! We will reply shortly!</span></p>");
jQuery("#send-message").fadeIn("slow");
}
});
Here is a link to the form... http://wordpress-dev.designer17.com/contact/ 这是表格的链接... http://wordpress-dev.designer17.com/contact/
The last time I used this it worked just fine, so I'm quite bewildered by this. 我上次使用它的时候效果很好,所以我对此感到很困惑。
Because you're using PHP within your .js file (line 56): 由于您在.js文件中使用PHP(第56行):
url: "<?php echo get_template_directory_uri(); ?>/mail-form.php",
What you can do is add this to your main template: 您可以将其添加到主模板中:
var TEMPLATE_URI = "<?php echo get_template_directory_uri(); ?>";
then within your .js file use: 然后在您的.js文件中使用:
url: TEMPLATE_URI + "/mail-form.php",
Your JavaScript, $.ajax function call in this case, is client side and is not parsed by php, which is server side. 您的JavaScript,在这种情况下,$。ajax函数调用是客户端,而不是php(服务器端)进行解析。 Because of this the JavaScript parser interprets the string "" as a literal string and it is not parsed.
因此,JavaScript解析器将字符串“”解释为文字字符串,因此无法解析。 To solve this simple use a relative url or generate the JavaScript in a php template which can parse the php code.
要解决此简单问题,请使用相对网址或在可解析php代码的php模板中生成JavaScript。
Remember: Code is always parsed/rendered serverside, sent back to the client's browser, then client side scripting, JavaScript code in your case, executes in the browser. 请记住:代码始终在服务器端进行解析/渲染,然后发送回客户端的浏览器,然后客户端脚本(在您的情况下为JavaScript代码)将在浏览器中执行。
I supose you moved your javascript onto a .js file, however unless you instruct your http server to process javascript files as php any php inside them is just text. 我认为您已将JavaScript移至.js文件,但是除非您指示HTTP服务器将javascript文件作为php处理,否则其中的任何php都是文本。
Either move the function back into a php file or (better approach) pass the URL as an attribute, for example: 将函数移回php文件或(更好的方法)将URL作为属性传递,例如:
// PHP FILE
<form action="URLGOESHERE" id="form_id">
...
// JAVASCRIPT FILE
var url = $("#form_id").attr("action");
jQuery.ajax({
type: "POST",
url: url,
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.