gpt4 book ai didi

php - AJAX 脚本的文件路径(在 Wordpress 中)

转载 作者:可可西里 更新时间:2023-10-31 22:40:02 24 4
gpt4 key购买 nike

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

    $.ajax({
url: process.php,
type: "POST",
data: data,
cache: false,
...

url 中,我调用了发送电子邮件的 php 文件,但仅当我指定完整路径时,ajax 才获取它:

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

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

url: "../../templates/process.php",

或使用变量在 html header/footer 中声明

HTML

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

脚本

url: "../../templates/process.php",

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

POST http://www.domain.com/templates/process.php 404 Not Found 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,
type: "POST",
cache: false,
data: data + '&action=sendmail' //action defines which function to use in add_action
});

在你的 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 in Plugins .

关于php - AJAX 脚本的文件路径(在 Wordpress 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16274093/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com