Once again I've been beating my head against the wall, trying to pull this part of returned data from ajax to a variable outside the function.
再一次,我一直在撞墙,试图将这部分返回的数据从ajax拉到函数外的变量。
When I return the value it always comes up undefined
when I alert()
inside it shows the proper values.
当我返回值时,它总是在我的alert()内部显示未定义时显示正确的值。
function getItemInfo(itemHashPass) {
$.ajax({
url: 'index.php//Welcome/getItem', //This is the current doc
type: "POST",
data: 'iHash='+itemHashPass,
dataType: "json",
async: false,
success: function(data){
return data.Response.data.inventoryItem.itemName;
}
});
}
I've also tried
我也试过了
function getItemInfo(itemHashPass) {
var tmp = null;
$.ajax({
url: 'index.php//Welcome/getItem', //This is the current doc
type: "POST",
data: 'iHash='+itemHashPass,
dataType: "json",
async: false,
success: function(data){
tmp = data.Response.data.inventoryItem.itemName;
}
});
return tmp;
}
1 个解决方案
#1
0
Like Jonathan said you should write your business logic in the callback or you can try Deferred syntax. In this case it will looks like
就像Jonathan所说,您应该在回调中编写业务逻辑,或者您可以尝试延迟语法。在这种情况下,它看起来像
function yourBusinnesLogicFunction() {
...
getItemInfo("password_hash_value").done(
function(data){
alert(data.Response.data.inventoryItem.itemName);
}
)
}
function getItemInfo(itemHashPass) {
var tmp = null;
return $.ajax({
url: 'index.php//Welcome/getItem', //This is the current doc
type: "POST",
data: 'iHash='+itemHashPass,
dataType: "json",
async: false,
})
}
#1
0
Like Jonathan said you should write your business logic in the callback or you can try Deferred syntax. In this case it will looks like
就像Jonathan所说,您应该在回调中编写业务逻辑,或者您可以尝试延迟语法。在这种情况下,它看起来像
function yourBusinnesLogicFunction() {
...
getItemInfo("password_hash_value").done(
function(data){
alert(data.Response.data.inventoryItem.itemName);
}
)
}
function getItemInfo(itemHashPass) {
var tmp = null;
return $.ajax({
url: 'index.php//Welcome/getItem', //This is the current doc
type: "POST",
data: 'iHash='+itemHashPass,
dataType: "json",
async: false,
})
}