2009-12-24 39 views
16

我在jQuery中做一些非常基本的事情時遇到問題。有人能告訴我我究竟做錯了什麼嗎?

如果我運行下面的代碼,函數$ .get似乎缺少(getJSON和其他人也缺少)。但$本身和其他函數確實存在,所以我知道JQuery正在加載。

google.load("jquery", "1.3.2"); 

function _validate(form, rules_file) { 
    $.get('/validation_rules.json',function(data) { 
    alert("hello") 
    }) 
} 

任何想法將不勝感激。

感謝, 羅布

編輯:這裏是一些額外的信息:

<script src="http://www.google.com/jsapi"></script> 
    <script> 
    google.load("prototype", "1.6"); 
    google.load("scriptaculous", "1.8"); 
    google.load("jquery", "1.3.2"); 
    </script> 
    <script> 
    jQuery.noConflict(); // prevent conflicts with prototype 
    </script> 
    <script src="/livepipe/src/livepipe.js" 
      type="text/javascript"></script> 
    <script src="/livepipe/src/window.js" 
      type="text/javascript"></script> 
    <script src="/livepipe/src/tabs.js" 
      type="text/javascript"></script> 
    <script src="/jquery.maskedinput-1.2.2.js" 
     type="text/javascript"></script> 

回答

15

您所擁有的$變量來自Prototype-js,因爲您使用的是jQuery.noConflict方法。

該方法將恢復$變量返回到任何庫首次實現它。

您應該直接使用jQuery全局對象jQuery開發方法,例如:

jQuery.get(/* .. */); 
jQuery.getJSON(/* .. */); 
// etc... 

或者,如果你願意,你可以定義另一個短變量作爲別名:

var $j = jQuery.noConflict(); 
2

如果指定jQuery.noConflict(),然後$不再提供jQuery的。改用jQuery.get。

2

你也可以將你的jQuery函數封裝在一個閉包中,並以「$」符號的形式傳遞jQuery,例如:

(function($) { 
    function _validate(form, rules_file) { 
     $.get('/validation_rules.json',function(data) { 
     alert("hello") 
     }) 
    } 
})(jQuery) 
38

如果使用jQuery的SLIM版本,也會發生這種情況。

<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script> 

給我

>jQuery.get 
:undefined 
>jQuery.get() 
:VM7130:1 Uncaught TypeError: jQuery.get is not a function 
    at <anonymous>:1:8 

在加載相同的庫沒有苗條選項

<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> 

作品以及

jQuery.get() :對象{readyState:1}

+1

JQuery網站上的基本用法示例說使用jquery-x.x.x.slim.min.js,顯然這對AJAX調用不起作用。謝謝! – bcarroll 2018-01-17 04:25:36