【发布时间】:2011-07-01 22:22:27
【问题描述】:
我正在使用对外部 API 的跨域 Ajax 请求。每隔一段时间,它就会失败,并显示控制台消息:
Uncaught TypeError: Property 'photos' of object [object DOMWindow] is not a function
查看返回的 JSON 是有效的 JSON,所以不是外部 API 的错。
我无法可靠地重现错误:似乎触发错误的唯一因素是我快速重复地调用请求。
在这种情况下,当用户移动 Google 地图(以向地图添加标记)时,我会调用 Ajax 请求,如果用户移动得太快就会发生这种情况。
以下是我的代码的相关部分:
// 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);
});
Google 了一下,错误Uncaught TypeError: Property 'photos' of object [object DOMWindow] is not a function 通常似乎是在没有加载 jQuery 时发生的。
但是,我认为这与此处无关,因为该函数是从地图的 tilesloaded 事件中调用的 - 它通常不会在第一次触发,它往往会在五六次快速调整地图大小后触发。所以如果它工作一次,页面肯定不会“忘记”jQuery?
感谢您的建议。
【问题讨论】:
-
尝试删除
jsonpCallback: 'photos',行,看看是否有效。最好让 jQuery 处理这些事情。当您使用它时,您真的需要这样的contentType参数集吗?那是发送数据,而不是接收数据... -
这只是意味着照片不是一个函数/方法因此你没有提供回调,所以你应该有
function photos(){...}
标签: javascript jquery ajax jsonp