jQuery getScript が解析エラー例外を返す
-
27-10-2019 - |
質問
2つのスクリプトをロードしようとしています $.getScript
Google Mapスクリプトを取得する関数。それがロードされた後、別のスクリプトを取得します(goMap
) マップ アプレットを簡単に作成できます。
ただし、ロードされると、Google Map API を取得する最初のスクリプトは正常ですが、2 番目のスクリプトは解析エラーを返し、次のように表示されます。
タイプエラー:「未定義」はコンストラクターではありません」
しかし、それがどこから参照しているのか、どの行を参照しているのかわかりません。おそらくこのファイルでジオコーダーを実行しようとしていると思います(次の最初の行) (function($){
:
これが私のコードです:
$.getScript('http://maps.google.com/maps/api/js?sensor=true').done(function()
{
$.getScript('../js/gomap.js').done(function()
{
// this never gets called
alert('gomap loaded');
}).fail(function(jqxhr, settings, exception)
{
alert(exception); // this gets shown
});
}).fail(function()
{
alert('failed to load google maps');
});
設定するAJAX設定を変更してみました async
に false
, 、しかし、まったく役に立ちませんでした。
解決
このエラーは、Google Maps API が次を使用してヘッドにロードされることを期待しているために発生します。 <script src="http://maps.google.com/maps/api/js?sensor=true"></script>
.
何らかの理由でそれができない場合でも、まだ希望はあります。Google Maps API は、以下を使用するため機能しません。 document.write
ページに依存関係を挿入します。コードを機能させるには、ネイティブのコードを上書きします。 document.write
方法。
デモ: http://jsfiddle.net/ZmtMr/
var doc_write = document.write; // Remember original method;
document.write = function(s) {$(s).appendTo('body')};
$.getScript('http://maps.google.com/maps/api/js?sensor=true').done(function() {
$.getScript('../js/gomap.js').done(function() {
alert('gomap loaded');
document.write = doc_write; // Restore method
}).fail(function(jqxhr, settings, exception) {
alert(exception); // this gets shown
document.write = doc_write; // Restore method
});
}).fail(function() {
alert('failed to load google maps');
});
他のヒント
@lolwut試してみてください
$.getScript('http://maps.google.com/maps/api/js?sensor=true').done(function()
{
alert(11);
$.getScript('http://www.pittss.lv/jquery/gomap/js/jquery.gomap-1.3.2.js').done(function()
{
// this never gets called
alert('gomap loaded');
}).fail(function(jqxhr, settings, exception)
{
alert(exception); // this gets shown
});
}).fail(function()
{
alert('failed to load google maps');
});
これが機能する場合は、相対パスです ../js/gomap.js 間違っている!
所属していません StackOverflow