duanjucong3124 2013-10-16 15:55 采纳率: 100%
浏览 45
已采纳

与wordpress的jquery POST错误

I'm trying to use POST to retrieve some scripts on a wordpress site but I'm getting the following error

Uncaught TypeError: Cannot call method 'post' of undefined 

Basically I load an external javascript file which loads a couple of scripts, then calls back to a function on the original page to execute a POST call to retrieve some data.

I've used this exact same script on a "regular" HTML based site, so I know it works elsewhere. jQuery is loaded as you can see, because I use .getScript to retrieve another file (which does load)

Now for the function retrieve_window(); located on the template file.

//template file that initially loads
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://localhost/Project1/js/retrieve_scripts.js"></script>
<div class="reserve_wrapper " id="resere_wrapper"></div>
<script>

    function retrieve_window(){

        $.post("http://localhost/Project1/windows/serve_window",function(data) {
            $('#reserve_wrapper').html(data);
            display_window();
        });
    }
</script>

Here's the get_scritps file that is loaded

//retrieve_scripts.js
jQuery(document).ready(function ($) {   
    $.getScript("http://localhost/project1/js/date.js", function() { }); //this script loads
    retrieve_window(); //calls back to template file to make POST request
});

So basically the retrieve_window is a callback function to ensure the date.js script is properly loaded

  • 写回答

2条回答 默认 最新

  • duanmou9228 2013-10-16 16:09
    关注

    The $ variable referenced in the retrieve_scripts.js file is the first argument passed into your .ready(function( $ ) {}). This is consistent with jQuery usage.

    The $ referenced in your inline script tag refers to a global $ variable since there are no other variables in scope with that name.

    Somewhere in code that is running but not in your excerpt the global $ reference to jQuery is getting removed, probably by calling jQuery.noConflict().

    Presumably at the time your inline script executes and defines the retrieve_window() function the global reference is still present. This is a hack, but I'm guessing will work:

    (function( $ ) {
        // we are in a closure but want this function to be global, so assign it globally
        retrieve_window = function() {
            // your code here
            $.post() etc. etc.
        };
    } ( $ )); // capture a reference to the global value of $ now before it is removed
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 vscode开发micropython,import模块出现异常
  • ¥20 Excel数据自动录入表单并提交
  • ¥30 silcavo仿真,30分钟,只需要代码
  • ¥15 FastReport 怎么实现打印后马上关闭打印预览窗口
  • ¥15 利用3支股票数据估计其均值和方差的95%置信区间。
  • ¥15 微信小程序运行一项功能时,弹出未知错误弹框,检查代码没有问题
  • ¥15 ATAC测序生成self-pseudo replicates之前是否要进行去线粒体reads
  • ¥15 python模糊字匹配函数问题
  • ¥20 谁刷目标页面的uv记录器上数据,数据只记录跳转的数值
  • ¥30 数据库软件的安装方法
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部