duanjucong3124 2013-10-16 23: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-17 00: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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器