【发布时间】:2019-03-10 14:18:36
【问题描述】:
从 ajax “成功”调用包含 ajax 的函数时的奇怪行为。
jQuery.ajax(
{
url: urlWMS,
dataType: 'jsonp',
jsonpCallback: 'parseResponse',
error: function(xhr, status, error) {
console.log("err: " + error);
},
success: function(jsonp) {
if(jsonp.features.length > 0) {
objResultats = [];
for(i = 0; i < jsonp.features.length; i++) {
objResultats.push({
carrer: jsonp.features[i].properties.des,
numero: sNumero,
idnom: jsonp.features[i].id.split('.')[1],
punt: jsonp.features[i].geometry.coordinates[0][0].toString()
});
}
}
if(sNumero.length > 0) {
//buscar punts exactes
for(i = 0; i < objResultats.length; i++) {
getPoint(i, objResultats[i].idnom, objResultats[i].numero); <----- this is the function that makes another call that randomly returns error
}
}
}
函数“getPoint”包含一个ajax调用:
Query.ajax(
{
url: urlWMS,
//type: 'GET',
dataType: 'jsonp',
jsonpCallback: 'parseResponse',
error: function(xhr, status, error) {
console.log("err: " + error);
},
success: function(jsonp) {
//strRespostaJSON = JSON.stringify(jsonp);
if(jsonp.features.length > 0) {
sPuntExacte = jsonp.features[0].geometry.coordinates.toString();
}
}
}
所以,我遇到的问题是函数“getPoint”返回例如 5 个点,但有时它随机返回 4 ok 和 1 错误,或 3 ok 2 错误。 控制台显示:
This time fail 3 times(of 5 calls)
This time fail 2 times(of 5 calls)
如果错误是随机的,那很奇怪......我认为这是在调用之间放置一个睡眠,因为我在某处读到调用重复相同的 jsonpCallback 会产生一些问题,是真的吗?
【问题讨论】:
标签: jquery ajax callback jsonp