为什么我的变量保持未定义状态,而我无法为我的item对象设置值? iv阅读了有关ajax javascript回调的整个互联网,我遵循了以下示例: How do I return the response from an asynchronous call? 但我仍然不知道自己在做什么错,请帮助我疯狂...我尝试了很多事情。
var nomclass = this.valueOf()
var id = nomclass.replace('itemid-', '')
var prix;
var remise;
var result;
function myCallback(result) {
var prix = result[0]
var remise = result[1]
console.log(result) // ["44,00", "30"]
}
foo(myCallback);
console.log(result) // undefined
function foo(callback) {
$.ajax({
url: 'getinfo.php',
type: 'POST',
data: {
id: id
},
dataType: 'json',
success: callback
})
};
var nombre = $('.geDiagramContainer .' + this).length;
var item = {
id: id,
class: nomclass,
prix: prix,
remise: remise,
nombre: nombre
}
console.log(item) // {id: "892", class: "itemid-892", prix: undefined, remise: undefined, nombre: 1}
itemsarr.push(item);