AJAX脚本的文件路径(在WordPress中)

我使用jquery ajax脚本发送电子邮件:

$.ajax({
url:process.php,
类型:“POST”,
数据:数据,
cache:false,
...

url中,我调用了发送电子邮件的php文件,但ajax只有在指定完整路径时才能获取该文件:

url:http://www.domain.com/wp-content/themes/site_theme/templates/process.php",

但我必须使用这样的语法:

url:“../../templates/process.php”,

或者使用变量在html页眉/页脚中声明

Html

<script type=“text/javascript”>
var urlMail='<?php bloginfo('template_url');?>/templates/process.php';
</script>

脚本

url:“../../templates/process.php”,

但在上述两种情况下,浏览器控制台都会检索此错误:

POSThttp://www.domain.com/templates/process.php 404找不到1.56s

我错在哪里

这不是wordpress中实现ajax的方法。所有ajax请求都应该发送到admin ajax.php

在模板文件中:

<script type=“text/javascript”>
var ajaxurl='<?php echo admin_url('admin-ajax.php');?>';
</script>

在您的js中:

$.ajax({
url:ajaxurl,
类型:“POST”,
cache:false,
data:data+'&action=sendmail'//action定义在add\u操作中使用的函数
});

在functions.php中:

函数发送我的邮件(){
#做你的事
}
添加操作(“wp\u ajax\u sendmail”、“发送我的邮件”);
添加操作(“wp\u ajax\u nopriv\u sendmail”,“发送我的邮件”);

阅读插件中的Ajax

发表评论