Wordpress中的Ajax不调用php函数 [英] ajax in wordpress not calling php function

查看:75
本文介绍了Wordpress中的Ajax不调用php函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道是否有人可以提供帮助;我正在尝试通过jquery在wordpress模板中的表单上实现一些ajax.

wonder if anyone can help; I'm trying to implement some ajax through jquery onto a form in a wordpress template.

jquery正在工作,我可以在sucess:部分中记录一条控制台消息,但是当它应该调用php函数时(当前在同一页面上,数据为0,我可以称其为直接)

The jquery is working, and the I can log a console message in the sucess: section, but the data is 0, when it should be calling the php function (at the moment on the same page, and I can call this directly)

所以我猜想jquery正在工作,正在调用admin-ajax,只是没有调用php函数.有什么想法我可能做错了吗?我不完全了解钩子,所以也许这是一个问题-我需要将某物钩到某个地方吗?

so I guess the jquery is working, the admin-ajax is being called, its just the php function is not being called. Any ideas what I could be doing wrong ? I don't fully understand hooks, so perhaps that is an issue - that I need to hook something in somewhere?

jquery(域将替换注释)

jquery (domain would replace comments)

<script type="text/javascript">
    jQuery(function ($) {
        $( "#movies" ).autocomplete({
                minLength:2,
                delay:500,
                source: function( request, response ) {
                    $.ajax({
                        type: 'POST',
                        url: "http://<!--domain here -->/wp-admin/admin-ajax.php",
                        dataType: 'json',
                        data: {
                            action: 'getMoviesForCode',
                            searchString: $("#movies").val()
                        },
                        success: function( data ) {
                            response(data);
                            console.log('jjj'+data);
                        }
                    });
                }           
        });
    });
    </script> 

php函数(在同一页面上)

php function (on same page)

<?php

    function getMoviesForCode(){
echo "
        <script type=\"text/javascript\">
        alert(\"hh\");
        </script>
    ";
   $searchString = $_POST['searchString'];
   $results = va_getMoviesForCode($searchString);  
  $results = json_encode($results);
  die($results);
}
 ?>

谢谢

推荐答案

您做错了.您的php函数应该位于主题的functions.php文件中.

You're doing it wrong. You php function should be in your theme's functions.php file.

然后应将函数挂接到wp_ajax_[your_action]wp_ajax_nopriv_[your_action].

You should then hook the function to wp_ajax_[your_action] and wp_ajax_nopriv_[your_action].

functions.php中应包含的内容的示例:

Example of what should be in your functions.php :

function getMoviesForCode(){
echo "
        <script type=\"text/javascript\">
        alert(\"hh\");
        </script>
    ";
   $searchString = $_POST['searchString'];
   $results = va_getMoviesForCode($searchString);  
  $results = json_encode($results);
  die($results);
}
add_action('wp_ajax_getMoviesForCode', 'getMoviesForCode');
add_action('wp_ajax_nopriv_getMoviesForCode', 'getMoviesForCode');

这篇关于Wordpress中的Ajax不调用php函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆