简体   繁体   English

Uncaught TypeError: Property … is not a function - 页面加载后

[英]Uncaught TypeError: Property … is not a function - after page has loaded

I'm using a cross-domain Ajax request to an external API.我正在使用对外部 API 的跨域 Ajax 请求。 Every so often it fails, with the console message:每隔一段时间,它就会失败,并带有控制台消息:

Uncaught TypeError: Property 'photos' of object [object DOMWindow] is not a function

Looking at the JSON being returned, it is valid JSON, so it is not the fault of the external API.查看返回的 JSON 是有效的 JSON,所以不是外部 API 的故障。

I can't reproduce the error reliably: the only factor that seems to trigger the error is when I call the request quickly and repeatedly.我无法可靠地重现错误:似乎触发错误的唯一因素是我快速重复地调用请求时。

In this case I'm calling the Ajax request when the user moves a Google map (to add markers to the map), and it occurs if the user moves around too quickly.在这种情况下,当用户移动 Google map(以向地图添加标记)时,我将调用 Ajax 请求,并且如果用户移动得太快就会发生这种情况。

Here are the relevant parts of my code:以下是我的代码的相关部分:

// Code located inside an external JS file referenced inside the head
// Not wrapped inside document.ready - but all the code setting up 
// the map (calling a function which calls a function which adds the 
// tilesloaded listener) *is* inside document.ready
function addMarkers() {
    var pm_url = "http://www.cyclestreets.net/api/photos.json?key=" + MY_KEY;
    $.ajax({
       url: pm_url,
       crossDomain: true, 
       contentType: "application/json",
       dataType: 'jsonp',
       data: pmdata,
       jsonpCallback: 'photos',
       success: function(data) {
        // TBA
       },
       error: function() {
           alert("Sorry, error retrieving photos!");
       }
   });
}
 google.maps.event.addListener(map, 'tilesloaded', function() {
 addMarkers(map);
 });

Having Googled a bit, the error Uncaught TypeError: Property 'photos' of object [object DOMWindow] is not a function generally seems to occur when jQuery has not been loaded.谷歌了一下,错误Uncaught TypeError: Property 'photos' of object [object DOMWindow] is not a function通常似乎在 jQuery 未加载时发生。

However, I don't see that this is relevant here, because the function is called from the map's tilesloaded event - and it doesn't generally fire the first time, it tends to fire after five or six rapid map resizes.但是,我认为这与这里无关,因为 function 是从地图的 tilesloaded 事件中调用的——它通常不会第一次触发,它往往会在五六次快速 map 调整大小后触发。 So if it works once, the page surely can't have 'forgotten' about jQuery?所以如果它工作一次,页面肯定不会“忘记”jQuery?

Thanks for your advice.谢谢你的建议。

If you want to specify the name of the function that jQuery creates from your success handler, but not actually define a separate function to use , you should use jsonp: 'photos' instead of jsonpCallback: photos .如果您想指定 jQuery 从您的success处理程序创建的 function 的名称,但实际上并未定义单独的 function 来使用您应该使用jsonpCallback: photos jsonp: 'photos' . Currently it's using photos in the URL which means it's calling photos({...data... }) when the JSONP response comes back, and that doesn't exist.目前它在 URL 中使用photos ,这意味着当 JSONP 响应返回时它正在调用photos({...data... }) ,但这并不存在。 Using the jsonp option on $.ajax() would create it.$.ajax()上使用jsonp选项会创建它。 You have a few options here.您在这里有几个选择。

You can do this (in a global scope) either of these two ways:您可以通过以下两种方式之一(在全局范围内)执行此操作:

function addMarkers() {
    var pm_url = "http://www.cyclestreets.net/api/photos.json?key=" + MY_KEY;
    $.ajax({
       url: pm_url,
       crossDomain: true, 
       contentType: "application/json",
       dataType: 'jsonp',
       data: pmdata,
       jsonpCallback: 'photos',
       error: function() {
           alert("Sorry, error retrieving photos!");
       }
   });
}
function photos(data) {
    // TBA
}

Or, what you intended I think:或者,我认为你的意图是什么:

function addMarkers() {
    var pm_url = "http://www.cyclestreets.net/api/photos.json?key=" + MY_KEY;
    $.ajax({
       url: pm_url,
       crossDomain: true, 
       contentType: "application/json",
       dataType: 'jsonp',
       data: pmdata,
       jsonp: 'photos',
       success: function(data) {
        // TBA
       },
       error: function() {
           alert("Sorry, error retrieving photos!");
       }
   });
}

....or just leave both off and let jQuery name the success callback itself (this happens by default, based on a timestamp). ....或者只是将两者都关闭,让 jQuery 自己命名success回调(默认情况下会发生这种情况,基于时间戳)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的TypeError:页面加载后无法将属性&#39;value&#39;设置为null - Uncaught TypeError: Cannot set property 'value' of null after the page loads 页面加载后反应调用功能 - react call function after page has loaded WordPress 页面:未捕获的类型错误:$ 不是 function - WordPress page: Uncaught TypeError: $ is not a function 在AngularJS中加载页面后触发jQuery函数 - Trigger jQuery function after page has loaded in AngularJS 未捕获的TypeError:对象#的属性“哪个” <Object> 不是功能 - Uncaught TypeError: Property 'which' of object #<Object> is not a function 未捕获的TypeError:对象#的属性getPos <Object> 不是功能 - Uncaught TypeError: Property getPos of object #<Object> is not a function 未捕获的TypeError:对象[对象全局]的属性'$'不是函数? - Uncaught TypeError: Property '$' of object [object global] is not a function? 未捕获的TypeError:无法读取函数中未定义的属性“ 0” - Uncaught TypeError: Cannot read property '0' of undefined at function 未捕获的TypeError:对象[object DOMWindow]的属性不是函数 - Uncaught TypeError: Property of object [object DOMWindow] is not a function 未捕获的TypeError:对象[对象DOMWindow]的属性&#39;$&#39;不是函数 - Uncaught TypeError: Property '$' of object [object DOMWindow] is not a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM