ajax返回的是一个对象吗,jQuery ajax()在成功时将json对象返回给另一个函数会导致错误...

我在这个问题上遇到了一个小时。我认为这是与变量范围有关的事情?无论如何,这是代码:

function loadRoutes(from_city)

{

$.ajax(

{

url: './ajax/loadRoutes.php',

async : true,

cache : false,

timeout : 10000,

type : "POST",

dataType: 'json',

data :

{

"from_city" : from_city

},

error : function(data)

{

console.log('error occured when trying to load routes');

},

success : function(data)

{

console.log('routes loaded successfully.');

$('#upperright').html(""); //reset upperright box to display nothing.

return data; //this line ruins all

//this section works just fine.

$.each(data.feedback, function(i, route)

{

console.log("route no. :" + i + " to_city : " + route.to_city + " price :" + route.price);

doSomethingHere(i);

});

}

});

}

每个部分在成功回调区域内工作正常。我可以看到Firebug控制台输出路径ID完全没有问题。

为了解耦目的,我认为最好只将JSON格式的数据对象返回给调用函数中的变量,如下所示:

//ajax load function

function findFromCity(continent, x, y)

{

console.log("clicked on " + continent + ' ' + x + ',' + y);

$.ajax(

{

url: './ajax/findFromCity.php',

async : true,

cache : false,

timeout : 10000,

type : "POST",

dataType : 'json',

data :

{

"continent" : continent,

"x" : x,

"y" : y

},

error : function(data)

{

console.log('error occured when trying to find the from city');

},

success : function(data)

{

var cityname = data.from_city;

//only query database if cityname was found

if(cityname != 'undefined' && cityname != 'nowhere')

{

console.log('from city found : ' + cityname);

data = loadRoutes(cityname);

console.log(data);

}

}

});

}

然后突然间,一切都停止了! Firebug控制台将数据对象报告为“未定义”...还没有由方法loadRoutes(cityname)中的返回对象分配?

抱歉,我对javascript的整体知识非常有限,所以现在我就像一个“模仿”,以业余的方式处理我的代码。

编辑:看过尼克的暗示,让我现在就开始研究它,看看它是怎么回事。

编辑第2期:

忍受我,仍然坚持这个:

//ajax load function

function findFromCity(continent, x, y)

{

console.log("clicked on " + continent + ' ' + x + ',' + y);

var cityname = "nowhere"; //variable initialized.

$.ajax(

{

url: './ajax/findFromCity.php',

async : true,

cache : false,

timeout : 10000,

type : "POST",

dataType : 'json',

data :

{

"continent" : continent,

"x" : x,

"y" : y

},

error : function(data)

{

console.log('error occured when trying to find the from city');

},

success : function(data)

{

cityname = data.from_city;

//only query database if cityname was found

if(cityname != 'undefined' && cityname != 'nowhere')

{

console.log('from city found : ' + cityname);

//data = loadRoutes(cityname);

//console.log(data);

}

}

});

return cityname; //return after ajax call finished.

}

Firebug控制台打印出一些有趣的东西:

nowhere

from city found : Sydney

我认为订单应该至少像这样颠倒:

from city found : Sydney

nowhere

那么,基本上,成功区域中定义的变量与外部相同变量的范围完全不同?这听起来对我来说很奇怪,但现在我看到了。

不过,不知道如何将json对象从成功回调中传递出来,将其分配给另一个变量......

结论:好吧,我得到了它,正在研究“通过引用传递”来利用副作用来改变现在由函数参数传递的变量......这与这个问题没有直接关系。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值