文件路径AJAX脚本(在Word preSS) [英] File path for AJAX script (in Wordpress)
问题描述
我用这个jQuery,AJAX脚本来发送电子邮件:
I use this jquery-ajax script to send email:
$.ajax({
url: process.php,
type: "POST",
data: data,
cache: false,
...
在网址
我打电话,发送电子邮件的PHP文件,但阿贾克斯得到它只有当我指定的完整路径:
in url
I call the php file that sends email, but ajax get it only if I specify the full path:
url: "http://www.domain.com/wp-content/themes/site_theme/templates/process.php",
但我必须使用的语法如下:
but I have to use a syntax like this:
url: "../../templates/process.php",
或者使用一个变量在HTML页眉/页脚声明
or using a variable to declare in the html header/footer
HTML
<script type="text/javascript">
var urlMail = '<?php bloginfo('template_url'); ?>/templates/process.php';
</script>
剧本
url: "../../templates/process.php",
但与上述两种情况下,浏览器控制台检索此错误:
but with both the above cases the browser console retrieves this error:
POST http://www.domain.com/templates/process.php 404 Not Found 1.56s
我在哪里错了?
Where am I wrong?
推荐答案
这并不是实现Ajax中字preSS的方式。所有的Ajax请求提出,要管理-ajax.php
。
That's not the way to implement ajax in wordpress. All ajax request should be made to admin-ajax.php
.
在你的模板文件:
<script type="text/javascript">
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
</script>
在你的JS:
$.ajax({
url: ajaxurl,
type: "POST",
cache: false,
data: data + '&action=sendmail' //action defines which function to use in add_action
});
在你的functions.php:
in your functions.php:
function send_my_mail(){
#do your stuff
}
add_action('wp_ajax_sendmail', 'send_my_mail');
add_action('wp_ajax_nopriv_sendmail', 'send_my_mail');
阅读有关 阿贾克斯插件
这篇关于文件路径AJAX脚本(在Word preSS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!