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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么