【问题标题】:ajax is not calling function, gives wrong responseajax 没有调用函数,给出错误的响应
【发布时间】:2014-03-11 13:47:55
【问题描述】:

我正在尝试在 wordpress 插件中进行 ajax 调用,这是构造函数的代码!

//动作

 add_action('init', array($this,'plugin_license_init'));
    add_action('wp_ajax_nopriv_plicense_getlicense', 'plicense_ajax_getlicense');
    add_action('wp_ajax_plicense_getlicense', 'plicense_ajax_getlicense');

<script src="http://code.jquery.com/jquery-1.11.0.min.js" type="text/javascript">

        </script>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js "></script>
        <script type="text/javascript">
                    jQuery(document).ready(function($){

//on button click event

        $("#regenerate_license").click(function() 
        {
                //$('.plic-loading-icon').show();
            // jquery.post has 3 parameter

            $.post( ajaxurl, {'action': "plicense_ajax_getlicense"}, function(response) 
                     {
                        alert(response);

                        $("#license_key").val(response);
                        //$('.plic-loading-icon').hide();
                     }
                 );
    });
});
        </script>

//我要调用的函数

public function plicense_ajax_getlicense() 
    {

        global $plicense_settings;
        //$this->plicensing_generate_guid( );
        echo "hey";
    }

//当按钮被点击时……我得到的响应是

<style>         .h{             width:98%;              height:4em;             border-style:solid ;................       {                        alert(response);                        $("#license_key").val(response);                        //$('.plic-loading-icon').hide();                     }                 );    });});        </script>       0

它是同一个html页面的源代码

【问题讨论】:

  • 那么你想要什么作为响应?
  • 我有一个 $this->plicensing_generate_guid( );我在上面评论过的函数......而不是它应该发送“嘿”字符串......但它没有
  • ajaxurl 定义在哪里?
  • 不知道! :P 但它应该使用 admin-ajax.php
  • 尝试硬编码所需的路径,而不是使用ajaxurl。如果可行,那么您需要弄清楚为什么 ajaxurl 没有预期值。

标签: jquery ajax wordpress plugins


【解决方案1】:

您可以从两个 jQuery 库中删除一个库:

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script  src="http:.../libs/jquery/1.9.1/jquery.min.js "></script>

您可以删除第二个。

【讨论】:

  • event.returnValue 已弃用。请改用标准 event.preventDefault()。这是控制台中的警告
【解决方案2】:

稍微改变一下你的鳕鱼 更改 jQuery 中的操作函数,这将解决问题

$.post( ajaxurl, {'action': "plicense_getlicense"}, function(response) 
  {
         alert(response);
         $("#license_key").val(response);
         //$('.plic-loading-icon').hide();
   }
   );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多