douhan8610 2015-10-23 13:03
浏览 69
已采纳

Wordpress插件无法接收ajax发布数据

I am trying to develop a WordPress plugin that will send a variable to my server. So far if I use my code bellow as separate PHP file and not within my script works great.. it sends the variables and receive the results from remote PHP file.

If I put the same code in my custom WordPress plugin it doesn't receive anything.

my code:

<script>
    jQuery(document).ready(function(){     
        jQuery(".adminpnlnshbutton").click(function(){
            var usermail = jQuery('#uemail').val();
            var userkey = jQuery('#ukey').val();
            var dataString = 'usermail='+ usermail + '&userkey='+ userkey;
            $.ajax({
                type: "POST",
                url: "http://www.myremotedomain.co.uk/check.php",
                data: dataString,
                crossDomain: true,
                dataType: 'html',
                success: function(data) {
                    alert (data);
                }
            });
        });
    });
</script>

And check PHP:

$m = $_POST['usermail'];
$k = $_POST['userkey'];
$s = " | ";
echo $m . $s . $k;

As WordPress plugin:

<?
    add_action('admin_menu', 'test_plugin_setup_menu');

    function test_plugin_setup_menu(){
            add_menu_page( 'Test Plugin Page', 'Tes Plugin', 'manage_options', 'test-plugin', 'test_init' );
    }

    function test_init(){
        if (is_user_logged_in()) { 
?>
    <input name="uemail" type="text" id="uemail" value="email" />
    <input name="ukey" type="text" id="ukey" value="activation key" />
    <div class="adminpnlnshbutton">SEND</div>
    <script>
        jQuery(document).ready(function(){     
            jQuery(".adminpnlnshbutton").click(function(){
                var usermail = jQuery('#uemail').val();
                var userkey = jQuery('#ukey').val();
                var dataString = 'usermail='+ usermail + '&userkey='+ userkey;
                $.ajax({
                    type: "POST",
                    url: "http://www.myremotedomain.co.uk/check.php",
                    data: dataString,
                    crossDomain: true,
                    dataType: 'html',
                    success: function(data) {
                    alert (data);
                    }
                });
            });
        });
    </script>
<?
    }
    }
?>

any idea why I can't receive the data inside my plugin?

  • 写回答

1条回答 默认 最新

  • dongyumiao5210 2015-10-23 13:06
    关注

    Solved... i had to add jquery

    function pw_loading_scripts_wrong() {
        echo '<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>';
    }
    add_action('admin_head', 'pw_loading_scripts_wrong');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎样采集全国最新个体工商户数据
  • ¥20 我是一名大学生,想学习java是自学还是报培训班呢
  • ¥15 pycharm该如何爬取网易云歌曲下的评论?
  • ¥15 java调用sap webservice问题
  • ¥20 如何完全复现何恺明resnet论文里的结果,有代码吗,获得论文中的结果(如下图)
  • ¥15 mysql如果表行行数小于1000条还需要分区吗
  • ¥20 有关matlab自定义特殊算符的问题
  • ¥15 第15届蓝桥杯选拔赛斗鱼养殖场
  • ¥15 在vivado的FIFO使用中,FIFO有没有提供可以输出内部存储空间状态的端口?
  • ¥15 C++ QT内存数据匹配对比指导